6

我正在開發我的第一款Android應用,以使用Google Cloud Messaging(GCM)服務進行推送通知。我已經到了可以從我的服務器應用程序成功發送消息的地步,並將消息的內容記錄在客戶端應用程序的GCMIntentService類中的onMessage事件中。但是我沒有在設備上看到收到消息的任何可視指示。我正在等待消息出現在手機上的下拉通知列表中,就像它在iPhone上一樣。這是否必須手動編碼?還有一種顯示消息的常用方法,無論當前哪個活動處於活動狀態,並且該應用程序在後臺是否處於空閒狀態?任何幫助讚賞。Android - 未出現在通知列表中的GCM推送通知

回答

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

+0

這是太棒了,非常感謝。 –

+0

嗨,我已經使用這個插件https://github.com/marknutter/GCM-Cordova,並遵循所有說明。然而,我只能在應用程序內獲取消息,但無法讓應用程序在android通知區域顯示通知。請指教。我使用這個插件,因爲我使用phonegap @Zachary Moshansky –

+0

對不起,但我從來沒有使用Phonegap,我無法評論如何使通知使用它 –

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()); 
    }