Android Notification的使用

今天使用4.0.3使用android

Notification notification2 = new Notification(R.drawable.advise2,  
"通知测试",
System.currentTimeMillis()); notification2.setLatestEventInfo(getActivity(), "testTitle", "testContent", null);ide

结果androidstudio报错,setLatestEventInfo该方法找不到,通过查证官方在API Level 11中,该函数已经被替代,不推荐使用了。因此在4.0.3平台也就是API Level 15中,使用Notification的setLatestEventInfo()函数时,显示setLatestEventInfo()效果。建议使用Notification.Builder来建立 notification 实例函数

Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
builder1.setSmallIcon(R.drawable.advise2); //设置图标
builder1.setTicker("显示第二个通知"); 
builder1.setContentTitle("通知"); //设置标题
builder1.setContentText("点击查看详细内容"); //消息内容
builder1.setWhen(System.currentTimeMillis()); //发送时间
builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光
builder1.setAutoCancel(true);//打开程序后图标消失
Intent intent =new Intent (MainActivity.this,Center.class);
PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
builder1.setContentIntent(pendingIntent);
Notification notification1 = builder1.build();
notificationManager.notify(124, notification1); // 经过通知管理器发送通知

若是该通知只是起到 “通知”的做用,不但愿用户点击后有相应的跳转,那么,intent,pendingIntent这几行代码能够不写测试

Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.advise);
 builder.setTicker("显示第一个通知");
builder.setContentTitle("第一个通知");
builder.setContentText("天天进步一点点");
builder.setWhen(System.currentTimeMillis()); //发送时间
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
notificationManager.notify(123, notification);

第一个具备点击提示有跳转功能,后面一个没有跳转功能,只是提示做用ui

如下借鉴其余博主的总结:
    低于API Level 11版本,也就是Android 2.3.3如下的系统中,setLatestEventInfo()函数是惟一的实现方法。前面的有关属性设置这里就再也不提了,网上资料不少。this

Intent  intent = new Intent(this,MainActivity);  
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
notification.setLatestEventInfo(context, title, message, pendingIntent);          
manager.notify(id, notification); spa

    高 于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可以使用 Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在 notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。事件

Notification.Builder builder = new Notification.Builder(context)  
            .setAutoCancel(true)  
            .setContentTitle("title")  
            .setContentText("describe")  
            .setContentIntent(pendingIntent)  
            .setSmallIcon(R.drawable.ic_launcher)  
            .setWhen(System.currentTimeMillis())  
            .setOngoing(true);  
notification=builder.getNotification(); ip

    高于API Level 16的版本,就能够用Builder和build()函数来配套的方便使用notification了。get

Notification notification = new Notification.Builder(context)    
         .setAutoCancel(true)    
         .setContentTitle("title")    
         .setContentText("describe")    
         .setContentIntent(pendingIntent)    
         .setSmallIcon(R.drawable.ic_launcher)    
         .setWhen(System.currentTimeMillis())    
         .build();  

    【注意点】:
    在构造notification的时候有不少种写法,可是要注意,用
Notification notification = new Notification();
这种构建方法的时候,必定要加上notification.icon这个设置,否则,程序虽然不会报错,可是会没有效果。

Example:Notification的使用案例:

public class MainActivity extends Activity {

    private Button bt1;
    private NotificationManager notificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);


        setViews();
        setlisteners();
    }

    private void setlisteners() {
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 发送通知
                Notification.Builder builder = new Notification.Builder(MainActivity.this);
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setTicker("this is ticker text"); // 通知的ticker内容,当通知刚建立的时候,它会在系统的状态栏一闪而过
                builder.setContentTitle("this is title"); // 通知的标题
                builder.setContentText("this is content"); // 通知内容
                builder.setWhen(System.currentTimeMillis()); // 通知被建立的时间,以毫秒为单位
                builder.setDefaults(Notification.DEFAULT_ALL); // 通知到来的时候,手机会发出振动、播放音乐、led灯亮的提示,具体是什么的提示,由手机的设置决定

                Intent intent = new Intent(MainActivity.this,NotificationActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
                builder.setContentIntent(pendingIntent); // 响应通知的点击事件
                Notification notification = builder.getNotification();
                notificationManager.notify(1, notification);
            }
        });
    }

    private void setViews() {
        bt1 = (Button)findViewById(R.id.bt1);
    }
}

NotificationActivity 通知的点击事件的响应活动:

public class NotificationActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_notification);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(1); // // 取消通知  参数:为建立通知的的通知id
}
}
相关文章
相关标签/搜索