簡體   English   中英

已收到GCM通知但未顯示

[英]GCM notification received but not displaying

我的Android應用程序和服務器已配置為接收和發送推送通知。 我的應用程序完美地接收通知,並且它在LogCat中顯示我的應用程序是打開的,在后台還是沒有正常運行。 但是,我在顯示它時遇到了問題。 無論我做什么,我都無法將其顯示在通知中心或作為振動手機或發出聲音的警報進入。

我錯過了什么? 我在這里使用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");
        }
    }
}

你很近,但你錯過了一步。 您的代碼准備通知但實際上並未顯示。 將這些行放在NotificationCompat.Builder代碼之后:

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

通知ID是一個任意數字,如果您以后需要更新或刪除通知,可以使用該數字來檢索通知。

使用此代碼! 希望代碼會幫助你

`

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

暫無
暫無

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

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