3

クリックするとアクティビティを開く通知を作成したいと思います。しかし、通知アクティビティをクリックすると開かれません。これが私のコードです:

NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(context, MessageReceivedActivity.class);
    intent.putExtra("payload", payload);
    intent.setAction(Long.toString(System.currentTimeMillis()));
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);

    Notification.Builder notification = new Notification.Builder(context)

    .setContentTitle("Message Received")
    .setSmallIcon(R.drawable.icon)
    .setContentText(payload)
    .setContentIntent(pendingIntent)
    .setAutoCancel(true);

    Notification notificationn = notification.getNotification();

    notificationManager.notify(0, notificationn);
4

3 に答える 3

5

これを使って:

Notification.Builder notification = new Notification.Builder(context)
.setContentIntent(getDialogPendingIntent(Text, intentName));

private PendingIntent getDialogPendingIntent(String dialogText,
            String intentname) {
        return PendingIntent.getActivity(
                context,
                dialogText.hashCode(),
                new Intent(ACTION_DIALOG)
                        .putExtra(Intent.EXTRA_TEXT, dialogText)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .setAction(intentname), 0);
}

getDialogPendingIntent (テキスト、インテント名) :インテント名=com.yourProject.exrta.yourIntentName

addFlags必要に応じて変更することもできputExtraます。


Call with Intent Name が機能しない場合は、そのようなクラスで使用してください。機能する必要があります。

Intent notificationIntent = new Intent(MainActivity.this, TestActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                            Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);

NotificationManager notificationManager = (NotificationManager) MainActivity.this
                        .getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder notification = new Notification.Builder(MainActivity.this)
                .setContentTitle("Message Received")
                .setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(true)
                .setContentIntent(intent);

Notification notificationn = notification.getNotification();
notificationManager.notify(1, notificationn);
于 2013-04-13T09:50:24.463 に答える
2

これを試して:

NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(context, MessageReceivedActivity.class);
intent.putExtra("payload", payload);
intent.setAction(Long.toString(System.currentTimeMillis()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
        intent, PendingIntent.FLAG_CANCEL_CURRENT);

Notification.Builder notification = new Notification.Builder(context)
.setContentTitle("Message Received")
.setSmallIcon(R.drawable.icon)
.setContentText(payload)
.setContentIntent(pendingIntent)
.setAutoCancel(true);

Notification notificationn = notification.build();
notificationManager.notify(YOUR_NOTIF_ID, notificationn);
于 2013-04-13T09:21:57.650 に答える
0

これは私のために働いた:

Context context1 = loggedin.this.getApplicationContext();

NotificationManager notificationManager = (NotificationManager)context1.getSystemService(NOTIFICATION_SERVICE);

Notification updateComplete = new Notification();

updateComplete.icon = android.R.drawable.stat_notify_sync;

updateComplete.tickerText = context1.getText(R.string.notification_title);

updateComplete.when = System.currentTimeMillis();

Intent notificationIntent = new Intent(context1,loggedin.class);

PendingIntent contentIntent = PendingIntent.getActivity(context1, 0,notificationIntent, 0);

String contentTitle = context1.getText(R.string.notification_title).toString();

String contentText;

contentText = context1.getText(R.string.notification_info_success).toString();
updateComplete.setLatestEventInfo(context1, contentTitle,contentText, contentIntent);

int LIST_UPDATE_NOTIFICATION = 0;
notificationManager.notify(LIST_UPDATE_NOTIFICATION, updateComplete);
于 2014-06-23T08:30:07.017 に答える