android 2.3 上的推送通知崩溃。
Posted
技术标签:
【中文标题】android 2.3 上的推送通知崩溃。【英文标题】:Push notifications on android 2.3 crashed. 【发布时间】:2013-03-13 09:52:41 【问题描述】:我正在尝试将推送通知发送到带有 GCM 的手机,它在带有 android 4 的设备上运行良好,但是当我尝试在 android 2.3 上发送通知时,应用程序崩溃了,我收到了这个错误
03-13 11:44:25.994: E/AndroidRuntime(3579): FATAL EXCEPTION: IntentService[GCMIntentService-1074787013996-1]
03-13 11:44:25.994: E/AndroidRuntime(3579): java.lang.IllegalArgumentException: contentIntent required: pkg=com.itom.vreauRCA id=0 notification=Notification(vibrate=default,sound=default,defaults=0xffffffff)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.os.Parcel.readException(Parcel.java:1251)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.os.Parcel.readException(Parcel.java:1235)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.app.NotificationManager.notify(NotificationManager.java:110)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.app.NotificationManager.notify(NotificationManager.java:90)
03-13 11:44:25.994: E/AndroidRuntime(3579): at com.itom.vreauRCA.GCMIntentService.generateNotification(GCMIntentService.java:71)
03-13 11:44:25.994: E/AndroidRuntime(3579): at com.itom.vreauRCA.GCMIntentService.onMessage(GCMIntentService.java:36)
03-13 11:44:25.994: E/AndroidRuntime(3579): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.os.Looper.loop(Looper.java:123)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.os.HandlerThread.run(HandlerThread.java:60)
03-13 11:44:26.374: V/GCMBroadcastReceiver(3579): onReceive: com.google.android.c2dm.intent.RECEIVE
03-13 11:44:26.374: V/GCMBroadcastReceiver(3579): GCM IntentService class: com.itom.vreauRCA.GCMIntentService
03-13 11:44:26.374: V/GCMBaseIntentService(3579): Acquiring wakelock
03-13 11:44:26.414: V/GCMBroadcastReceiver(3579): onReceive: com.google.android.c2dm.intent.RECEIVE
03-13 11:44:26.414: V/GCMBroadcastReceiver(3579): GCM IntentService class: com.itom.vreauRCA.GCMIntentService
03-13 11:44:26.414: V/GCMBaseIntentService(3579): Acquiring wakelock
这是我的 GCMBaseIntentService 类
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService
private static final String TAG = "GCMIntentService";
PendingIntent contentIntent;
public GCMIntentService()
super(GetObiecte.SENDER_ID);
@Override
protected void onRegistered(Context context, String registrationId)
Log.i(TAG, "Device registered: regId = " + registrationId);
Log.d("GCMIntentService", "in GCMIntentService");
@Override
protected void onUnregistered(Context context, String registrationId)
Log.i(TAG, "Device unregistered");
@Override
protected void onMessage(Context context, Intent intent)
Log.i(TAG, "Received message");
String a = intent.getStringExtra("data");
String b = intent.getStringExtra("data2");
generateNotification(context, a,b);
@Override
protected void onDeletedMessages(Context context, int total)
Log.i(TAG, "Received deleted messages notification");
@Override
public void onError(Context context, String errorId)
Log.i(TAG, "Received error: " + errorId);
@Override
protected boolean onRecoverableError(Context context, String errorId)
Log.i(TAG, "Received recoverable error: " + errorId);
return super.onRecoverableError(context, errorId);
private void generateNotification(Context context, String message,
String title)
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "i-Asigutare";
notification.defaults = Notification.DEFAULT_ALL;
notification.setLatestEventInfo(context, title, message, null);
notificationManager.notify(0, notification);
【问题讨论】:
【参考方案1】:问题在于 notification.setLatestEventInfo 方法。
此方法是 API 级别 11 中的 deprecated,鼓励使用 Notification.Builder。您还可以查看 NotificationCompat.Builder 以了解支持库的使用情况。
如果您仍想按照自己的方式行事,则应查看此问题的已接受答案: Android - notification manager, having a notification without an intent
清楚地说明这样使用
notification.setLatestEventInfo(context, contentTitle, contentText, PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0));
【讨论】:
【参考方案2】:notification.setLatestEventInfo(context, title, message, null);
而不是null
,您必须添加一个 Intent,当用户触摸通知时,它将启动。
来自android developer page:
contentIntent:当用户点击展开的通知时启动的意图。如果这是一个活动,它必须包含 FLAG_ACTIVITY_NEW_TASK 标志,这要求您按照任务和返回堆栈文档中的说明进行任务管理。
【讨论】:
以上是关于android 2.3 上的推送通知崩溃。的主要内容,如果未能解决你的问题,请参考以下文章
从通知托盘按下推送通知时,Android GCM 应用程序崩溃