java - android notification pendingintent onNewintent 未调用

标签 java android

我想在按下 fcm 通知时调用 onNewIntent() 方法。

我阅读了一些关于此的问答,我认为我的代码变得简单了。

但它不起作用。我不知道怎么了:(

这是我的代码。

FCM:

private void sendPushNotification(String message) {

    System.out.println("received message :" + message);
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.untitled)
            .setContentTitle("This is title")
            .setContentText(message)
            .setPriority(Notification.PRIORITY_MAX)
            .setVibrate(new long[]{0,500})
            .setContentIntent(pendingIntent);


    PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
    wakelock.acquire(5000);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

onNewIntent() :

@Override
public void onNewIntent(Intent intent){
    super.onNewIntent(intent);
    setIntent(intent);
    Bundle extras = intent.getExtras();
    if(extras != null){

          Toast.makeText(getApplicationContext(),"Hello!",Toast.LENGTH_LONG).show();

    }
}

和 list :

    <activity android:name=".MainActivity"
        android:launchMode="singleTop">

logcat 中只有两个错误:

07-25 18:38:18.216 25648-25648/? E/合子:v2

07-25 18:38:18.221 25648-25648/? E/合子:访问信息:0

最佳答案

重新发布我在这里找到的解决方案: click on notification to go current activity

添加这些行:

intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

我不知道为什么必须设置它们,但如果您不设置它们,将不会调用 onNewIntent。

关于java - android notification pendingintent onNewintent 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45298615/

相关文章:

android - Navigator.pop(context) 抛出异常

java - @Sql 不起作用 @BeforeTransaction Spring 测试注解

java - 重新渲染丰富:tabPanel without losing data

java - 是否有对象更改跟踪/版本控制 Java API?

java - 将业务逻辑放在监听器/执行器类中是一种不好的做法吗?

android - Android 12 中的 MediaSessionCompat 行为发生变化,setPlaybackToRemote 停止正常工作

android - YouTube Android Player API 的配额限制

android - 在由两个不同 Activity 调用的 DialogFragment 中,对于这种情况,您如何转换回调用 Activity ?

android - 自定义 'Keyboard' 内置在 Android 上的应用程序中

java - 如何按照我添加的顺序获取 JInternalFrame