在活动中获取通知消息

Posted

技术标签:

【中文标题】在活动中获取通知消息【英文标题】:Get Notification Message in Activity 【发布时间】:2017-08-10 21:33:27 【问题描述】:

我允许用户在Notification 上执行Tap,然后在Launching Appopening 上执行特定的Activity

在我打开的那个Activity 中,我想得到我通过通知发送的message

SendNotification(...) 方法:

private void sendNotification(String messageTitle, String messageBody, Bitmap image, String TrueOrFalse) 

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("AnotherActivity", TrueOrFalse);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setLargeIcon(image)/*Notification icon image*/
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image))/*Notification with Image*/
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

AnotherActivity.java:

public class AnotherActivity extends AppCompatActivity 

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

        setContentView(R.layout.activity_another);

        // here I would like to get value of messageBody
    


更新 #1

sendNotification(...)

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("YouTubeActivity", TrueOrFalse);
    intent.putExtra("message", messageTitle);

    ....

    

AnotherActivity.javaonCreate() 中尝试了两种方式,但没有收到消息

        String message=getIntent().getStringExtra("message");
        Toast.makeText(YouTubeActivity.this, message, Toast.LENGTH_LONG).show();

        Bundle extras = getIntent().getExtras();
        if (extras != null) 
            String value = extras.getString("message");
            Toast.makeText(YouTubeActivity.this, value, Toast.LENGTH_LONG).show();
            //The key argument here must match that used in the other activity
        

【问题讨论】:

【参考方案1】:

这样做:

Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("AnotherActivity", TrueOrFalse);
    intent.putExtra("message", messageBody);

所以你的代码是这样的:

private void sendNotification(String messageTitle, String messageBody, Bitmap image, String TrueOrFalse) 

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("AnotherActivity", TrueOrFalse);
    intent.putExtra("message", messageBody);


    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setLargeIcon(image)/*Notification icon image*/
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image))/*Notification with Image*/
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

要获取数据,请执行以下操作:

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

        setContentView(R.layout.activity_another);

        // here I would like to get value of messageBody
        String message=getIntent().getStringExtra("message");
    

【讨论】:

尝试收不到消息 当你点击通知启动 MainActivity 或 AnotherActivity ? AnotherActivity.java (好的,我现在知道了,我必须调用 AnotherActivity.java 而不是 MainActivity.jav)让我试试这个....过去两天我太愚蠢了想为什么我没有收到消息.. 无论如何先让我试试看 你可以得到这个intent.putExtra("AnotherActivity", TrueOrFalse); ? 完成...我只是使用了 AnotherActivity.java 而不是 MainActivity.java...谢谢

以上是关于在活动中获取通知消息的主要内容,如果未能解决你的问题,请参考以下文章

从通知发送消息和标题,以在活动的textview中显示

点击打开登录活动而不是主要活动的通知

从 GCM 推送通知打开地图活动

如何通过单击 firebase 通知打开新活动并在 textview 上打印通知消息

在 ios 中获取活动的本地通知警报类型或本地通知警报视图按钮单击事件

Parse.com:在 onPushOpen 上获取 Pushnotification 消息