【问题标题】: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
        }

【问题讨论】:

    标签: android firebase android-intent push-notification


    【解决方案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...谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多