我为GCM推送通知编写了IntentService。 我收到消息,但显示我的通知给用户时出现了问题。 这里是我的代码:Android通知未显示
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级别是我的这个应用程序的最低要求。
如果您拖动通知栏,该怎么办?文字是否显示在那里? “不......通知......弹出”是什么意思? –
当我向下拖动通知栏时,文本正在显示。这是一个正常的行为为Android?对不起,我是一名iPhone用户,并且是android开发人员的新手。我认为,如果你与建设者建立一个消息,就像在iOS屏幕上的弹出窗口... – user4878754
是的,这是正常的和正确的。我会建议尽快阅读更多有关Android UI设计模式的文章。这两个系统之间有很多不同之处,Android用户不想看到iOS设计模式(反之亦然)。我已经在下面解释了一些。 –