Android,发送推送通知给自己

时间:2014-06-20 16:22:47

标签: android push-notification

我希望用户为自己发送推送通知。

有没有办法在不使用Google Cloud Messaging的情况下实现这一目标?

离线,只需在自己的手机上显示通知。

感谢。

1 个答案:

答案 0 :(得分:3)

构建和发送通知的示例:

来自:http://www.vogella.com/tutorials/AndroidNotifications/article.html

// prepare intent which is triggered if the
// notification is selected

Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

// build notification
// the addAction re-use the same intent to keep the example short
Notification n  = new Notification.Builder(this)
    .setContentTitle("New mail from " + "test@gmail.com")
    .setContentText("Subject")
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(pIntent)
    .setAutoCancel(true)
    .addAction(R.drawable.icon, "Call", pIntent)
    .addAction(R.drawable.icon, "More", pIntent)
    .addAction(R.drawable.icon, "And more", pIntent).build();


NotificationManager notificationManager = 
  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, n); 

您可以根据需要编辑通知。

也很有帮助:

http://developer.android.com/training/notify-user/build-notification.html

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

http://developer.android.com/reference/android/app/NotificationManager.html

http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

快乐的编码......