2013-05-09 33 views
1

我的Android應用程序和服務器已配置爲接收和發送推送通知。我的應用程序完美收到通知,並在LogCat中顯示我的應用程序是否在後臺打開,或者不在運行。但是,我遇到問題需要顯示。無論我做什麼,我都無法將它顯示在通知中心,或者以警報的形式出現,讓手機振動或發出聲音。已收到但未顯示的GCM通知

我錯過了什麼?我從這裏使用GCM插件:https://github.com/marknutter/GCM-Cordova

我試過讓它使用NotificationCompat發送通知,但我一直不成功。

- >從GCM JSON傳遞給這個函數...

@Override 
protected void onMessage(Context context, Intent intent) { 
    Log.d(TAG, "onMessage - context: " + context); 

    // Extract the payload from the message 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     try 
     { 
      Log.v(ME + ":onMessage extras ", extras.getString("message")); 

      JSONObject json; 
      json = new JSONObject().put("event", "message"); 

      // My application on my host server sends back to "EXTRAS" variables message and msgcnt 
      // Depending on how you build your server app you can specify what variables you want to send 

      json.put("message", extras.getString("message")); 
      json.put("msgcnt", extras.getString("msgcnt")); 

      Log.v(ME + ":onMessage ", json.toString()); 

      GCMPlugin.sendJavascript(json); 
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.notification_icon) 
       .setContentTitle("TEST") 
       .setContentText("TEST"); 
      // Send the MESSAGE to the Javascript application 
     } 
     catch(JSONException e) 
     { 
      Log.e(ME + ":onMessage", "JSON exception"); 
     } 
    } 
} 
+0

的目標顯然是讓測試通知顯示,然後剝離該消息出來的JSON的。 – centree 2013-05-09 16:41:41

回答

3

你很接近,但你缺少一個步驟。你的代碼準備了一個通知,但並沒有真正顯示它。將這些行你NotificationCompat.Builder代碼之後:

final int notificationId = 1; 
NotificationManager nm = (NotificationManager) getApplicationContext() 
     .getSystemService(Context.NOTIFICATION_SERVICE); 
nm.notify(notificationId, mBuilder.build()); 

的通知ID是,如果您需要更新或在以後刪除它,你可以用它來檢索該通知的任意數目。

+0

它現在剛出現,沒有噪音或任何東西。當我點擊它時什麼也沒有發生 – centree 2013-05-09 19:38:49

+0

這些都可以通過通知生成器進行控制。查看[完整的api](http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html)以瞭解支持的內容。要獲得點擊處理,你需要使用'setContentIntent()'(這可能需要你深入研究Android的細節,而不是Cordova想象中的)。爲了獲得聲音,你需要調用'setSound()'並且在'res/raw'目錄下的一個聲音文件中給它一個Uri(不要在Uri中包含文件擴展名)。 – Barend 2013-05-09 21:11:15

+0

我發現你注意到npocmaka在標籤維基上的抄襲,同時也檢查了建議的修改,不幸的是很多人都沒有注意到:(你認爲我們應該怎麼做?我發送了一個「其他」的主持人標誌 – Doorknob 2013-05-17 21:11:04

0

使用此代碼!希望代碼將幫助您

`

Intent resultIntent = new Intent(this, MainActivity.class); 

    resultIntent.putExtra("msg", msg); 

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, 
      resultIntent, PendingIntent.FLAG_ONE_SHOT); 

    NotificationCompat.Builder mNotifyBuilder; 
    NotificationManager mNotificationManager; 

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    mNotifyBuilder = new NotificationCompat.Builder(this) 
      .setContentTitle("Alert") 
      .setContentText("You've received new message.") 
      .setSmallIcon(R.mipmap.ic_launcher); 
    // Set pending intent 
    mNotifyBuilder.setContentIntent(resultPendingIntent); 

    // Set Vibrate, Sound and Light 
    int defaults = 0; 
    defaults = defaults | Notification.DEFAULT_LIGHTS; 
    defaults = defaults | Notification.DEFAULT_VIBRATE; 
    defaults = defaults | Notification.DEFAULT_SOUND; 

    mNotifyBuilder.setDefaults(defaults); 
    // Set the content for Notification 
    mNotifyBuilder.setContentText("New message from Server"); 
    // Set autocancel 
    mNotifyBuilder.setAutoCancel(true); 
    // Post a notification 
    mNotificationManager.notify(notifyID, mNotifyBuilder.build()); 
`