简体   繁体   中英

Access the Push notification content after a click on the push message (Android)

i got an Android app, which is receiving push messages.

If the user is clicking on the push message, the activity GoToNotification is called. This is working so far.

But how can I access the content of the push message? The Bundle.extra? I am trying, to react on some content of the push message, but the bundle extra is always null.

here my code so far:

private void sendNotification(String msg, Bundle extras) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, GoToNotification.class), 0);
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.icon)
                    .setContentTitle("Warning")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setExtras(extras)
                    .setContentText(msg);

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


public class GoToNotification extends Activity {

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

        Bundle extras = this.getIntent().getExtras();
        if (extras != null) {
            System.out.println(extras.toString());
        } else {
            System.out.println("extras is null");
        }
    }
}
Intent notificationIntent = new Intent(context, SampleActivity.class);
notificationIntent.putExtra("EXTRA_ID", "ur data");

and in ur activity

if (extras != null) {
 ID = extras.getString("EXTRA_ID");
}

Thanks, I managed to solve the problem, here is the code:

As written already, i need to put the extra content. It's also important to set the requestId, it was not working without it.

Here is the code working:

private void sendNotification(String msg, String dpId) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, GoToNotification.class);
    intent.putExtra("dpId", dpId);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    int requestID = (int) System.currentTimeMillis();
    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,
            intent, 0);
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.icon)
                    .setContentTitle("Test")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(Integer.parseInt(dpId), mBuilder.build());
}

public class GoToNotification extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState); String regid;

       Intent intent = getIntent();
       if (intent != null) {
            if (intent.hasExtra("dpId")) {
                 System.out.println("Klick auf push dpid:" + intent.getStringExtra("dpId"));

            }
        }
    }
}

Manifest:

  <activity android:name=".GoToNotification" android:label="@string/app_name" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM