已收到GCM通知但未显示

时间:2013-05-09 16:41:06

标签: android google-cloud-messaging

我的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");
        }
    }
}

2 个答案:

答案 0 :(得分:3)

你很亲密,但你错过了一步。您的代码准备通知但实际上并未显示。将这些行放在NotificationCompat.Builder代码之后:

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

通知ID是一个任意数字,如果您以后需要更新或删除通知,可以使用该数字来检索通知。

答案 1 :(得分: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());
`