gpt4 book ai didi

未显示 Android 通知

转载 作者:行者123 更新时间:2023-11-30 02:10:40 25 4
gpt4 key购买 nike

我为 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) 的 huawai u8666e。至少我希望这个 API 级别是我对这个应用程序的最低要求。

最佳答案

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

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

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


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

请参阅 Android 开发者文档:

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

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


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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com