简体   繁体   English

点击通知无法打开活动

[英]click on notification not opening the activity

what is wrong with my code below? 我下面的代码有什么问题? I expect the MainActivity is opened up after notification is touched/clicked. 我希望在触摸/单击通知后打开MainActivity。 my code is below: 我的代码如下:

private void sendNotification(Quote quote) {
    mNotificationManager = (NotificationManager)
           this.getSystemService(Context.NOTIFICATION_SERVICE);

    String message = quote.getQuote() + " - " + quote.getAuthor();

    // Creates an Intent for the Activity      
    Intent notifyIntent = new Intent(this, MainActivity.class);
    notifyIntent.putExtra(Intent.EXTRA_SUBJECT, DailyQuotes.NOTIFICATION_QOD_MODE);
    notifyIntent.putExtra(Intent.EXTRA_TEXT, quote.getQuoteID());
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);


    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle(getString(R.string.qod_title))
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(message))
    .setContentText(message);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(DailyQuotes.NOTIFICATION_QOD_ID, mBuilder.build());
}

Please can anyone help me on this. 请任何人可以帮助我。

try this: 尝试这个:

mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
int requestID = (int) System.currentTimeMillis();
Intent notificationIntent = new Intent(this,
        MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(
        this, requestID, notificationIntent,
        PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentText("This is test");
mBuilder.setContentIntent(contentIntent);
mBuilder.setContentTitle("Title").setSmallIcon(
        R.drawable.ic_launcher);
mNotifyManager.notify(requestID, mBuilder.build());

you can change the flags. 您可以更改标志。

hope it helps. 希望能帮助到你。 i have changed the request id. 我已经更改了请求ID。

you should use unique id for each notification. 您应该为每个通知使用唯一的ID。 have a look over here. 在这里看看。 How to create Multiple statusbar Notifications in android 如何在Android中创建多个状态栏通知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 点击时打开GCM通知未打开指定的活动 - GCM Notification On Click not opening the specified Activity 单击推送通知时未打开新活动 - New Activity not opening on push notification click 单击打开登录活动而不是主要活动的通知 - Notification on click opening login activity instead of main activity 为什么通过pushwoosh通知点击打开活动在android中不起作用? - Why opening activity by pushwoosh notification click doesn't work in android? 点击GCM通知,无法在Android Kitkat中打开所需的活动 - Click on GCM notification not opening desired Activity in Android Kitkat 当应用程序在后台/未运行时,通知单击无法打开特定活动 - Notification click not opening specific activity when the app is in background/not running 当应用程序处于后台时如何在单击通知时打开目标活动 - how opening the targeted activity on click of notification when app is in background 带有按钮的通知,单击该按钮即可执行操作而不打开活动 - Notification with button that upon click take an action without opening activity 当应用程序处于后台/未运行时,不打开通知点击的特定活动 - Not opening specific activity on notification click when the app is in background/not running 通知未打开活动 onClick - Notification is not opening Activity onClick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM