Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 通知不打开Acivity onCLick大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个通知,点击它时应该打开活动.但是当我点击通知活动时没有打开.
任何帮助将不胜感激.这是我的代码

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,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);

解决方法

用这个

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);
}

如果需要,您可以更改addFlags或putExtra.

如果使用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,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);

大佬总结

以上是大佬教程为你收集整理的android – 通知不打开Acivity onCLick全部内容,希望文章能够帮你解决android – 通知不打开Acivity onCLick所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。