簡體   English   中英

顯示收到的推送通知

[英]Displaying Received Push Notifications

根據Google開發者指南 ,我正在實施這些代碼。

一切正常,我可以成功接收通知數據。

如果您檢查GCMIntentService.java文件的第70行,它會將這些行回顯到LogCat:

 I/GCM Demo(6653): Working... 1/5 @ 8656981
 I/GCM Demo(6653): Working... 2/5 @ 8657982
 I/GCM Demo(6653): Working... 3/5 @ 8658983
 I/GCM Demo(6653): Working... 4/5 @ 8659983
 I/GCM Demo(6653): Working... 5/5 @ 8660984
 I/GCM Demo(6653): Completed work @ 8661985
 I/GCM Demo(6653): Received: Bundle[{msg=messagehere, from=SENDERIDHERE, android.support.content.wakelockid=3, collapse_key=do_not_collapse}]

因此我的設備正在接收通知數據,但是設備狀態欄中沒有可見的通知。 沒事

您能告訴我為什么這些推送通知僅在LogCat中不顯示在我的設備上嗎?

更新

  1. 清單文件
  2. 這是我的sendNotification()方法:

     private void sendNotification(String msg) { mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setContentTitle("GCM Notification") .setStyle(new NotificationCompat.BigTextStyle() .bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } 

您可以嘗試這種方法嗎?...這個對我有用。 雖然與您幾乎一樣...

private void sendNotification(String msg)
{
    int uniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(this, uniqueId,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Hello")
            .setStyle(new NotificationCompat.BigTextStyle().bigText("Hello"))
            .setContentText("This is your notification content")
            .setAutoCancel(true)
    mBuilder.setContentIntent(contentIntent);

    mNotificationManager.notify(uniqueId, mBuilder.build());
}

喚醒電話是完全不同的事情。 您可以通過以下方式使用WakeLock進行操作:

設置通知時:

WakeLock screenOn = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "test");
screenOn.acquire();

是的,不要忘記在不再需要喚醒鎖時釋放喚醒鎖(可能在5秒鍾后或點擊通知;根據您的需要):

screenOn.release();

看起來(在您的android清單文件中)該服務可能尚未啟用

請嘗試將<service android:name=".GcmIntentService" />替換為:

<service android:enabled="true" android:name=".GcmIntentService" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM