3

我已經成功地設置了在android應用上通過phonegap運行GCM的代碼。我已經設法確保手機註冊ID,並能夠使用PHP腳本中的此ID嚮應用發送消息。 我唯一的問題是,當應用程序打開時,該消息顯示爲javascript警報,並且我正在查看將消息發送到手機的通知欄但未在頂部通知欄上顯示圖標。android GCM推送通知

**>做的是在頂欄可以顯示圖標,在Android設備

有誰知道如果PhoneGap的GCM該插件能夠這樣做的?**

我一直在使用C2DM-的PhoneGap。 https://github.com/marknutter/GCM-Cordova

請幫我的朋友...

+0

你能改寫你的問題更清晰?當收到推送通知時,您是否試圖在通知(頂部)欄中顯示圖標? – acj

+0

是的,當收到一個推送通知,然後在通知(頂部)欄中不顯示圖標 – Patel

+0

toadzky下面的答案是正確的。大多數圖書館都會讓您指定收到推送通知時要執行的操作。 – acj

回答

2

您正在使用需要指定收到消息時做什麼的庫。你可以修改他們的代碼來完成你想要的或者自己的GCMIntentService。

0

最快捷的方法是將以下代碼添加到GCMIntentService.java到onMessage函數中。您正在使用的GCM插件僅接收GCM消息,但您必須自行觸發通知。您還可以嘗試使用cordova插件來顯示狀態欄通知。

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) { 
    String message = extras.getString("message"); 
    String title = extras.getString("title"); 

    Notification notif = new Notification(R.drawable.ic_launcher, message, System.currentTimeMillis()); 
    notif.flags = Notification.FLAG_AUTO_CANCEL; 
    notif.defaults |= Notification.DEFAULT_SOUND; 
    notif.defaults |= Notification.DEFAULT_VIBRATE; 

    Intent notificationIntent = new Intent(context, SomeActivity.class); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

    notif.setLatestEventInfo(context, "Title of notification", message, contentIntent); 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns); 
    mNotificationManager.notify(1, notif); 
0

請檢查此代碼

私有靜態無效generateNotification(上下文語境,字符串消息){ INT圖標= R.drawable.ic_launcher;

 // here is your icon drawable instead of ic_launcher 

    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); 
    Intent notificationIntent = new Intent(context.getApplicationContext(), devicephp.class); 
    // set intent so it does not start a new activity 

    notificationIntent.putExtra("msg", message); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = 
      PendingIntent.getActivity(context, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(0, notification); 


}