未显示Android通知
我为GCM推送通知编写了一个IntentService。 我收到消息,但向用户显示通知时出现问题。 这是我的密码:未显示Android通知,android,notifications,google-cloud-messaging,android-notifications,android-notification-bar,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.Conte
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());
}
}
我看不出有错。我的意思是,我是从
该代码所做的唯一一件事是,手机的通知栏中显示了一个小图标(在本例中为“ic_notif”)。
但是没有向用户弹出文本或通知
我使用安卓工作室。
我的调试设备是带有android 4.0.3(API 15)的华外u8666e。
至少我希望这个API级别是我对这个应用程序的最低要求。你看到的是对于棒棒糖之前的版本,Android的正常设计行为 设计逻辑是,这种方法创建了一个更干净的界面,不会通过在用户面前放置弹出窗口来中断用户当前的操作。(关于哪种方法更好有很多争论——iOS弹出窗口还是Android通知) 当创建
通知时,棒棒糖会在设备窗口顶部创建一个小弹出窗口,从而稍微改变这一点
如果您真的想强制显示弹出对话框,您应该考虑设计一个“全屏”通知
请参阅Android开发者文档:
使用此方法,您可以使用所需的任何自定义布局创建新的活动
,并启动该活动,而不是将通知放置在状态栏中
(全面实施全屏通知将超出本文的范围)
我建议不要强制全屏通知,除非在极少数情况下,如闹钟或电话应用程序。相反,我建议您坚持Android的设计方式,并使用操作系统。如果您向下拖动通知栏会怎么样。文本显示在那里吗?“不……通知……弹出”是什么意思?当我向下拖动通知栏时,文本显示出来。这是android的正常行为吗?对不起,我是iPhone用户,是android开发的新手。我想,如果你用构建器构建一条消息,屏幕上会出现类似iOS的弹出窗口……是的,这是正常和正确的。我建议尽快阅读更多关于Android UI设计模式的文章。这两个系统之间有很多差异,Android用户不希望看到iOS设计模式(反之亦然)。我在下面解释了更多。谢谢你的回答。