android整合--notification推送通知

到目前为止,想必大家已经都熟悉使用Toast去给用户显示信息了。尽管使用Toast很方便,但是Toast显示的通知并不是永久存储的。它只在屏幕上显示一小段时间,然后就消失了。如果它包含一些特别重要的信息,如果用户没有观察屏幕,那么用户就很容易错过它。

对于那些重要的信息,应该采用一种更加持久保存的方法。在这种情况下,应该使用NotificationMnanger(消息管理器)去显示一个长久的信息,这个消息被显示在了StatusBar(状态栏)上面,使用用户能够很容易地看见。

接下来展示如何发送一个Notification通知。

首先建个工程Notification

先不解释 上代码

[java]  view plain copy
  1. public class NotificationsActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     int notificationId = 1;  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.           
  9.     }  
  10.       
  11.     public void onclick(View view){  
  12.         displayNotification();  
  13.     }  
  14.     public void displayNotification(){  
  15.         //设置点击通知跳转页面  
  16.         Intent intent = new Intent(this,NotificationView.class);  
  17.         intent.putExtra("notificationid", notificationId);  
  18.         //得到pendingintent  延迟执行intent  
  19.         PendingIntent pendintent = PendingIntent.getActivity(this0    , intent, 0);  
  20.         //获取通知管理器并设置图标与显示时间  
  21.         NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  22.         Notification notfi = new Notification(R.drawable.icon,"today there is a meeting",System.currentTimeMillis());  
  23.         //设置通知标题与内容  
  24.         CharSequence form = "System notification";  
  25.         CharSequence message = "Meeting is at 3pm";  
  26.         notfi.setLatestEventInfo(this, form, message, pendintent);  
  27.          //---100ms delay, vibrate for 250ms, pause for 100 ms and    
  28.         // then vibrate for 500ms---    
  29.         //震动提示  
  30.         notfi.vibrate = new long[]{100,250,100,500};  
  31.         nm.notify(notificationId, notfi);  
  32.     }  
  33. }  

点击通知转到这个activity

[java]  view plain copy
  1. public class NotificationView extends Activity {  
  2.   
  3.     /* (non-Javadoc) 
  4.      * @see android.app.Activity#onCreate(android.os.Bundle) 
  5.      */  
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         // TODO Auto-generated method stub  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.notification);  
  11.         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  12.         nm.cancel(getIntent().getExtras().getInt("notificationId"));  
  13.     }  
  14.   
  15.       
  16. }  

main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12.        <Button  
  13.         android:id="@+id/btnnoti"  
  14.         android:layout_width="fill_parent"   
  15.        android:layout_height="wrap_content"     
  16.        android:onClick="onclick"  
  17.        android:text="点击通知"  
  18.         />  
  19. </LinearLayout>  

notification.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent"  
  6.   android:orientation="vertical">  
  7.    <TextView    
  8.         android:layout_width="fill_parent"    
  9.         android:layout_height="wrap_content"    
  10.         android:text="Here are the details for the notification..." />    
  11. </LinearLayout>  

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.test.Notifications"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="7" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".NotificationsActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.   
  17.     <activity android:name=".NotificationView"  
  18.                    android:label="notification detail">  
  19.                    <intent-filter>  
  20.                        <action android:name="android.intent.action.MAIN" />  
  21.                       <category android:name="android.intent.category.DEFAULT" />  
  22.                    </intent-filter>  
  23.                    </activity>  
  24.     </application>  
  25.       <uses-permission android:name="android.permission.VIBRATE"/>   
  26. </manifest>  

大致思路为

1 设置通知跳转后的intent和activity,通知id

Intent intent = new Intent(this,NotificationView.class);
  intent.putExtra("notificationid", notificationId);

2 得到pendingintent(后篇讲一下pengdingintent 和intent的区别)

PendingIntent pendintent = PendingIntent.getActivity(this, 0    , intent, 0);

3     获取通知管理器并设置图标与显示时间
        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification notfi = new Notification(R.drawable.icon,"today there is a meeting",System.currentTimeMillis());

4     //设置通知标题与内容
        CharSequence form = "System notification";
        CharSequence message = "Meeting is at 3pm";
        notfi.setLatestEventInfo(this, form, message, pendintent);

5     //震动提示(可加可不加)
        notfi.vibrate = new long[]{100,250,100,500};
        nm.notify(notificationId, notfi);


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值