0

I have a FireBase Message. If it comes in, I show a Notification (works). When I click on the Notification it should open my application (works). Now I want to use putExtra(), to hand over some information to my app (that works only sometimes - if the app is in the foreground). That's my problem.

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

In the .MainActivity-Class:

@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();
Felix
  • 1
  • rndNo4Intent - why do you make this a random number? – Bö macht Blau May 28 '18 at 16:55
  • I've read, that the notification needs a unique number. So I use a randomnumber. I tried a lot to solve the problem. And I read many threads, but I found no solution. – Felix May 28 '18 at 18:11
  • But using a random number you may get the same number more than once – Bö macht Blau May 28 '18 at 18:19
  • Yes, with a probability from 1:100.000.000 - I think, that will work. But the random number shouldn't be the problem. – Felix May 28 '18 at 18:22
  • I just ran your code on an emulator (Android 7.0) and everything seems to be working: start App, sendNotification triggered by tapping on Button, now either leave app (and remove it from recent apps) or stay in app, in both cases open notification shade and tap on notification: app opens and Toast is displayed. – Bö macht Blau May 28 '18 at 18:46
  • If I switch to an other app or remove app from memory, I get only "null". Perhaps the problem is, that I worked with FireBase. I send the notification in the class: public class MyFirebaseMessagingService extends FirebaseMessagingService – Felix May 28 '18 at 19:05
  • But once you have created the Notification it should not matter how it was created. Now and then there are questions about Intent data which are getting lost, like [this one](https://stackoverflow.com/questions/18037991/pendingintent-does-not-send-intent-extras). Maybe one of the answers there will work for you – Bö macht Blau May 28 '18 at 19:12
  • Thank you for your help. Both solutions I already tested. Both uses "Notification notification", which is deprecated. – Felix May 28 '18 at 20:48

0 Answers0