我正在開發我的第一款Android應用,以使用Google Cloud Messaging(GCM)服務進行推送通知。我已經到了可以從我的服務器應用程序成功發送消息的地步,並將消息的內容記錄在客戶端應用程序的GCMIntentService類中的onMessage事件中。但是我沒有在設備上看到收到消息的任何可視指示。我正在等待消息出現在手機上的下拉通知列表中,就像它在iPhone上一樣。這是否必須手動編碼?還有一種顯示消息的常用方法,無論當前哪個活動處於活動狀態,並且該應用程序在後臺是否處於空閒狀態?任何幫助讚賞。Android - 未出現在通知列表中的GCM推送通知
6
A
回答
7
此代碼將在屏幕頂部的android系統欄中生成通知。此代碼將創建一個新的意圖,在用戶點擊頂部欄中的通知之後,該意圖將引導用戶轉到「Home.class」。如果您希望根據當前活動執行特定的操作,您可以將GCMIntentService的廣播請求發送到其他活動。
Intent notificationIntent=new Intent(context, Home.class);
generateNotification(context, message, notificationIntent);
private static void generateNotification(Context context, String message, Intent notificationIntent) {
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
注意,此示例使用R.drawable和R.String資源,這將需要存在於工作,但它應該給你的想法。有關狀態通知http://developer.android.com/guide/topics/ui/notifiers/index.html以及關於廣播接收者的更多信息,請參閱此處。 http://developer.android.com/reference/android/content/BroadcastReceiver.html
1
如果您正在使用GcmListenerService您可以使用此代碼,添加到您的onMessageReceived的sendNotification的()
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
sendNotification(message);
}
private void sendNotification(String message) {
Intent intent = new Intent(this, YOURCLASS.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_park_notification)
.setContentTitle("Ppillo Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
相關問題
- 1. GCM實現推送通知在Android
- 2. android GCM推送通知
- 3. Android推送通知GCM
- 4. 推送通知通過GCM
- 5. 未使用JSON的GCM推送通知
- 6. Android中的GCM推送通知
- 7. Android GCM推送通知沒有給出任何通知
- 8. 使用GCM推送通知
- 9. 使用GCM推送通知
- 10. GCM推送通知未復活
- 11. GCM推送通知的NodeJS
- 12. 未提供推送通知的GCM API
- 13. 在Android上啓動推送通知GCM
- 14. GCM android php android推送通知
- 15. Android上的推送通知 - 不是GCM
- 16. 使用gcm的android推送通知
- 17. 推送通知圖標GCM
- 18. GCM推送通知與Asp.Net
- 19. GCM推送通知與鈦
- 20. 錯誤:未註冊 - GCM推送通知
- 21. 在GCM中查看推送通知
- 22. android GCM推送通知崩潰
- 23. Appcelerator鈦Android推送通知GCM失敗?
- 24. Android GCM推送通知不起作用
- 25. Android替代GCM推送通知
- 26. Android推送通知GCM PHP和MySQL
- 27. Android推送通知:Google GCM與Amazon SNS?
- 28. 推送通知不出現
- 29. Worklight 6.3通過GCM推送通知未達到android設備
- 30. GCM Android推送通知:發送確定但未發送
這是太棒了,非常感謝。 –
嗨,我已經使用這個插件https://github.com/marknutter/GCM-Cordova,並遵循所有說明。然而,我只能在應用程序內獲取消息,但無法讓應用程序在android通知區域顯示通知。請指教。我使用這個插件,因爲我使用phonegap @Zachary Moshansky –
對不起,但我從來沒有使用Phonegap,我無法評論如何使通知使用它 –