1

私のアンドロイドアプリとサーバーは、プッシュ通知を送受信するように構成されています。私のアプリは完全に通知を受け取り、アプリがバックグラウンドで開いているか、正常に実行されていないかに関係なく、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

かなり近づいていますが、1 つのステップを逃しています。コードは通知を準備しますが、実際には表示しません。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 に答える