Android 通知未显示

标签 android notifications google-cloud-messaging android-notifications android-notification-bar

我为 GCM 推送通知编写了一个 IntentService。 我收到消息,但向用户显示通知时出现问题。 这是我的代码:

import com.google.android.gms.gcm.GoogleCloudMessaging;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;

public class GcmIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;


    public GcmIntentService() {
        super("GcmIntentService");
    }

    public static final String TAG = "GCM test";

    @Override
    protected void onHandleIntent(Intent intent) {
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);
        if (!intent.getExtras().isEmpty()) {  // has effect of unparcelling Bundle
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + intent.getExtras().toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " + intent.getExtras().toString());
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // Post notification of received message.
                sendNotification("message:\n" + intent.getStringExtra("message"));
                Log.i(TAG, "Received: " + intent.getExtras().toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String msg) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_notif)
                        .setContentTitle("My notification")
                        .setContentText(msg);
        Intent resultIntent = new Intent(this, PopupMessageActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(PopupMessageActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

我没有看到错误。我的意思是,我从 Androids developer guide 复制了代码.

此代码所做的唯一事情是小图标(在本例中为“ic_notif”)显示在手机的通知栏中。 但没有向用户弹出任何文本或通知。

我使用android-studio。 我的调试设备是带有 android 4.0.3 (API 15) 的华为 u8666e。 至少我希望这个 API 级别是我对此应用程序的最低要求。

最佳答案

您所看到的是 Lollipop 之前版本的正常设计的 Android 行为

设计逻辑是,此方法创建一个更干净的界面,并且不会通过在用户面前放置弹出窗口来中断用户当前的操作。 (对于哪种方法更好存在很多争论 - iOS 弹出窗口与 Android 通知)。

Lollipop 通过在创建通知时在设备窗口顶部创建一个小弹出窗口来稍微改变这一点。


如果您确实想强制显示弹出对话框,您应该考虑设计一个“全屏”通知。

请参阅 Android 开发人员文档:

使用此方法,您可以使用所需的任何自定义布局创建一个新的 Activity,然后启动它,而不是将通知放置在状态栏中。

(全屏通知的完整实现超出了本文的范围)


我建议不要强制全屏通知,除非在极少数情况下,例如闹钟或电话应用程序。相反,我建议您坚持 Android 的设计方式并与操作系统配合使用。

关于Android 通知未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30150191/

相关文章:

ios - 关闭的应用程序上的地理通知

android - 收到同样的消息

android - 推送通知提供商如何通过选择推送给用户?

java - 广播接收器无法接收谷歌云消息

java - 循环遍历 arrayList

安卓通知宽度

javascript - 谷歌云消息 (fake_message_id)

java - 动态访问java对象变量

android - 应用看不到模块的 Activity

android - 我在特定时间安排本地通知,使用开关但它不会在设定时间触发