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());
}
これは、 Android 4.1であるSamsung tab 2 ではうまく機能しますが、 Android 4.0であるSamsung Galaxy Sで通知が来ると、My-App-Name has crashedというエラーが表示されるだけです。そのため、通知は受信されていますが生成されていません。代わりに、通知が到着するとアプリケーションがクラッシュします。私が間違っていることはありますか?どんな助けでも大歓迎です。ありがとう。GCM