1

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

2 回答 2

3

你已经很接近了,但你错过了一步。您的代码准备了一个通知,但实际上并没有显示它。将这些行放在您的NotificationCompat.Builder代码之后:

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

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

于 2013-05-09T18:39:05.577 回答
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());
`
于 2015-11-02T07:14:36.747 回答