这两天公司项目用到消息推送机制,想将后台推送的消息以notification的形式展示个用户,自己也就试着这了几个小demo(包含兼容API11、API16及以后版本和自定义view),废话不多说,直接上代码,具体解释都写在注释中了
public class MainActivity extends AppCompatActivity {
public static TextView tvPayload ;
public static final int NOTIFICATION_API11 = 1;
public static final int NOTIFICATION_API16 = 2;
public static final int NOTIFICATION_CUSTOM = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 在布局文件中为每个按钮设置了onclick="click"
*/
public void click(View v){
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/**
* 设置意图,指向另一个activity,当点击notification的时候,会跳转过去
* action = "com.xiaohua.notification.MSG"
* category = "android.intent.category.DEFAULT"
*/
Intent intent = new Intent();
intent.setAction("com.xiaohua.notification.MSG");
intent.addCategory("android.intent.category.DEFAULT");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
switch (v.getId()){
case R.id.btn_notify11:
/**
* 创建一个PendingIntent ,和intent类似,由于不是马上调用,需要下拉状态发出的activity,所以采用的是PendingIntent,即点击Notification跳转到哪个acitivity
* context 上下文
* requestCode 请求码
* intent 打开其他界面的意图
* flags 用来标识延迟意图是否只能使用一次:FLAG_ONE_SHOT只能使用一次
*/
intent.putExtra("msg","XXX,我想你了");
PendingIntent pendingIntent11 = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notify11 = new Notification.Builder(this)
.setSmallIcon(R.drawable.notify_icon)
.setContentTitle("新消息title")
.setContentText("XXX,我想你了")
.setContentIntent(pendingIntent11)
.setNumber(3)
.getNotification();
notify11.flags = Notification.FLAG_AUTO_CANCEL;
notify11.defaults = Notification.DEFAULT_ALL;
manager.notify(NOTIFICATION_API11,notify11);
break;
case R.id.btn_notify16:
intent.putExtra("msg","这是在API 16下创建的notify跳转的界面");
PendingIntent pendingIntent16 = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notify16 = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notify_16_icon)
.setTicker("TickerText:你有新消息")
.setContentTitle("消息title")
.setContentText("消息content")
.setContentIntent(pendingIntent16)
.setNumber(2)
.build();
notify16.flags = Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTIFICATION_API16,notify16);
break;
case R.id.btn_mynotify:
intent.putExtra("msg","我是从自定义的notity跳转过来的");
Notification myNotify = new Notification();
myNotify.icon = R.drawable.notify_custom;
myNotify.tickerText = "我的自定义消息";
myNotify.when = System.currentTimeMillis();
myNotify.flags = Notification.FLAG_NO_CLEAR;
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_custom);
remoteViews.setTextViewText(R.id.tv_title,"自定义notify来了");
remoteViews.setImageViewResource(R.id.iv_icon,R.drawable.notify_custom);
myNotify.contentView = remoteViews;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_ONE_SHOT);
myNotify.contentIntent = pendingIntent;
manager.notify(NOTIFICATION_CUSTOM,myNotify);
break;
case R.id.btn_clear11:
manager.cancel(NOTIFICATION_API11);
break;
case R.id.btn_clear16:
manager.cancel(NOTIFICATION_API16);
break;
case R.id.btn_clear_custom:
manager.cancel(NOTIFICATION_CUSTOM);
break;
case R.id.btn_clear:
manager.cancel(NOTIFICATION_API11);
manager.cancel(NOTIFICATION_API16);
manager.cancel(NOTIFICATION_CUSTOM);
break;
default:
break;
}
}
}