读书人

android调整-notification推送通知

发布时间: 2013-09-17 13:35:59 作者: rapoo

android整合--notification推送通知

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

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

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

首先建个工程Notification

先不解释 上代码

[java] view plaincopy
  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. public void onclick(View view){
  10. displayNotification();
  11. }
  12. public void displayNotification(){
  13. //设置点击通知跳转页面
  14. Intent intent = new Intent(this,NotificationView.class);
  15. intent.putExtra("notificationid", notificationId);
  16. //得到pendingintent 延迟执行intent
  17. PendingIntent pendintent = PendingIntent.getActivity(this, 0 , intent, 0);
  18. //获取通知管理器并设置图标与显示时间
  19. NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  20. Notification notfi = new Notification(R.drawable.icon,"today there is a meeting",System.currentTimeMillis());
  21. //设置通知标题与内容
  22. CharSequence form = "System notification";
  23. CharSequence message = "Meeting is at 3pm";
  24. notfi.setLatestEventInfo(this, form, message, pendintent);
  25. //---100ms delay, vibrate for 250ms, pause for 100 ms and
  26. // then vibrate for 500ms---
  27. //震动提示
  28. notfi.vibrate = new long[]{100,250,100,500};
  29. nm.notify(notificationId, notfi);
  30. }
  31. }

点击通知转到这个activity

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

main.xml

[html] view plaincopy
  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 plaincopy
  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 plaincopy
  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. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".NotificationsActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. <activity android:name=".NotificationView"
  16. android:label="notification detail">
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.DEFAULT" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. <uses-permission android:name="android.permission.VIBRATE"/>
  24. </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);


读书人网 >Android

热点推荐