应用程序处于前台(运行)时不显示GCM通知

时间:2016-06-07 10:15:07

标签: android push-notification notifications google-cloud-messaging

我想gcm通知甚至,而应用是前景(在屏幕上)。通知来自应用关闭或后台。但当应用程序在屏幕上(打开)通知不会出现在顶部但我需要这个。

{编辑:当应用为前景时,我的代码中的BTW Log.d(TAG, "From: " + from);成功打印项目编号,但Log.d(TAG, "Message: " + message);打印null onMessageReceived 方法中的那些行)如您所知,通知不会显示在顶部。但是当app关闭时,这些日志永远不会打印,但会在顶部显示通知。我不明白为什么日志不打印? }

这是我的代码相关部分。我正在使用google's github project.

public class MyGcmListenerService extends GcmListenerService {

private static final String TAG = "MyGcmListenerService";

/**
 * Called when message is received.
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);



    if (from.startsWith("/topics/")) {
        // message received from some topic.
    } else {
        // normal downstream message.
    }

    // [START_EXCLUDE]
    /**
     * Production applications would usually process the message here.
     * Eg: - Syncing with server.
     *     - Store message in local database.
     *     - Update UI.
     */

    /**
     * In some cases it may be useful to show a notification indicating to the user
     * that a message was received.
     */
    sendNotification(message);
    // [END_EXCLUDE]
}
// [END receive_message]

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.
 */
private void sendNotification(String message) {
    final Intent notificationIntent = new Intent(this, AccountSummaryActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    Intent intent = new Intent(this, MainMenuActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Bitmap bmURL= BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_3_web_3);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setLargeIcon(bmURL)
            .setContentTitle("Diversity")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)

            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

有什么想法怎么做吗?提前谢谢。

0 个答案:

没有答案