Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有PendingEvent和putExtra don的Java通知';行不通_Android_Notifications - Fatal编程技术网

Android 带有PendingEvent和putExtra don的Java通知';行不通

Android 带有PendingEvent和putExtra don的Java通知';行不通,android,notifications,Android,Notifications,我有一个FireBase消息。如果它进来,我会显示一个通知(起作用)。当我点击通知时,它会打开我的应用程序(works)。现在,我想使用putExtra()将一些信息传递给我的应用程序(只有在应用程序位于前台的情况下,这种方法有时才有效)。那是我的问题 private void sendMyNotification(String message) { int rndNo4Intent = Math.round((float)(Math.random()*100000000));

我有一个FireBase消息。如果它进来,我会显示一个通知(起作用)。当我点击通知时,它会打开我的应用程序(works)。现在,我想使用putExtra()将一些信息传递给我的应用程序(只有在应用程序位于前台的情况下,这种方法有时才有效)。那是我的问题

   private void sendMyNotification(String message) {

    int rndNo4Intent = Math.round((float)(Math.random()*100000000));
    int rndNo4Msg = Math.round((float)(Math.random()*100000000));

    //On click of notification it redirect to this Activity
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("action","openWiki"+rndNo4Msg); // My Test-Data

    PendingIntent pendingIntent = PendingIntent.getActivity(this, rndNo4Intent , intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("CodingSpace")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);

    // Since android Oreo notification channel is needed. Sonst kommt nichts an.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "CodingSpaceChannel",
                NotificationManager.IMPORTANCE_HIGH);               // in Oreo ist High so, dass es einen Ton gibt
        notificationManager.createNotificationChannel(channel);
    }


   notificationManager.notify(rndNo4Msg, notificationBuilder.build());
}
在.main活动类中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...        


    Intent iin= getIntent();
    String msg = iin.getStringExtra("action");
    Toast.makeText(MainActivity.this,"get: "+ msg,Toast.LENGTH_SHORT).show();

rndNo4Intent-为什么要将其设置为随机数?我已经了解到,通知需要一个唯一的数字。所以我用了一个随机数。为了解决这个问题,我做了很多努力。我读了很多文章,但我没有找到解决办法。但是使用一个随机数,你可能会得到相同的数多于一次,概率为1:100.000.000,我想,这会起作用。但随机数应该不是问题。我刚刚在模拟器(Android 7.0)上运行了你的代码,一切似乎都正常:启动应用程序,点击按钮触发发送通知,现在要么离开应用程序(并将其从最近的应用程序中删除),要么留在应用程序中,在这两种情况下,打开通知阴影并点击通知:应用程序打开并显示Toast。