我正在尝试使用NotificationCompact.Builder
此处描述的类生成通知http://developer.android.com/guide/topics/ui/notifiers/notifications.html。
下面是我的代码
private void generateNotification(Context context, String message, String notification_type, String receiver_type) {
long time = System.currentTimeMillis();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My App")
.setWhen(time)
.setSound(alarmSound)
.setContentText(message);
// Creates an explicit intent for an Activity in your app
Intent resultIntent;
resultIntent = new Intent(context, SplashActivity.class);
if(notification_type.equalsIgnoreCase("message")){
if(receiver_type.equalsIgnoreCase("1"))
resultIntent = new Intent(context, MyMessageForTutorActivity.class);
else
resultIntent = new Intent(context, MyMessageActivity.class);
}
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(QuickNotesActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
这在我的Samsung tab 2即Android 4.1上效果很好,但是当我的Samsung Galaxy S上出现通知时,它只是显示错误My-App-Name has crashed。因此,通知正在通过但未生成,而是在通知到达时我的应用程序崩溃。有什么我做错了吗?非常感谢任何帮助。谢谢。GCM