繁体   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