单击推送通知时如何打开活动?

Posted

技术标签:

【中文标题】单击推送通知时如何打开活动?【英文标题】:How to open Activity when click on pushnotification? 【发布时间】:2016-08-31 06:50:31 【问题描述】:

目前我正在我的项目中处理推送通知。当推送通知显示我想在单击通知时打开 Mynotification 活动但它总是打开 MainActivity 为什么?我也为此谷歌搜索,但找不到正确的答案。

这是我的 MyFirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService 

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) 
        sendNotification(remoteMessage.getNotification().getBody());

    

    private void sendNotification(String messageBody) 


        Intent intent = new Intent(this, MyNotification.class);
        intent.putExtra("Message",messageBody);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                0);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Shri Sidhbali Baba")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0, notificationBuilder.build());
    

这是我的清单文件:

 <activity android:name=".MyNotification"
            android:parentActivityName=".MainActivity">
            <intent-filter>
                <action android:name=".MyNotification" android:label="@string/app_name"/>

                <category android:name="android.intent.category.DEFAULT"/>


            </intent-filter>

        </activity>

单击哦推送通知时如何打开 Mynotification 活动。 感谢您的宝贵时间...

【问题讨论】:

【参考方案1】:

AFAIK,这段代码在应用程序处于前台时运行。当它在后台时,Firebase 会将通知传送到系统托盘。我遇到过类似的问题。当它传递到系统托盘时,它总是打开 MainActivity。我注意到 firebase 允许将参数传递给 Intent。我利用这种技术来读取自定义字符串(从 Firebase 消息控制台打开高级选项并将键指定为“项目”,将值指定为“MyNotification”)。 从主活动中读取此字符串,并将控件从您的 MainActivity::onCreate() 方法重定向到适当的活动

//private String ITEM = "item";
@Override
protected void onCreate(Bundle savedInstanceState) 
    //Parse the input passed to the activity
    Intent intent = getIntent();
    String input = intent.getStringExtra(ITEM);
    //Redirect to MyNotification
    Intent redirect = new Intent(this, MyNotification.class);
    startActivity(redirect);

【讨论】:

在我的 FCM 控制台中,高级选项是删除应用程序...你能告诉我如何添加密钥 我指的是来自 console.firebase.google.com->Notification 的“新消息”点击“高级选项”,您会在下面看到转换事件。这是用于向您的应用发送通知的同一控制台 不适合我。它显示错误无法开始活动。意图为空。【参考方案2】:

更新这个:

 private void sendNotification(String messageBody) 


    Intent intent = new Intent(this, MyNotification.class);
    intent.putExtra("Message",messageBody);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MyNotification.class), 0);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Shri Sidhbali Baba")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationBuilder.setContentIntent(contentIntent);

    notificationManager.notify(0, notificationBuilder.build());

【讨论】:

添加这一行 notificationBuilder.setContentIntent(contentIntent); setContentIntent(pendingIntent);是更改为 setContentIntent(contentIntent); 嘿,我在上面添加此行 notificationBuilder.setContentIntent(contentIntent); 让我们continue this discussion in chat。【参考方案3】:

尝试在您的清单中删除此行

android:parentActivityName=".MainActivity"

编辑 1:您可以在此处阅读更多内容 https://developer.android.com/training/implementing-navigation/ancestral.html

原因是当您调用“MyNotification”活动时,后台堆栈会“自动”为您将其父活动添加到后台堆栈,因此您可以按返回返回您的“MainActivity”

【讨论】:

@MinWan 的任何原因?【参考方案4】:

试试这个

public class VideoNotification extends AppCompatActivity 
    public Bitmap image ,bmp;
    public NotificationCompat.Builder nb;
    final String url = "https://www.google.es/images/srpr/logo11w.png";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_notification);


    

    public void createNotification(View view) 
        // Prepare intent which is triggered if the
        // notification is selected
        Intent intent = new Intent(this, VideoActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

        nb = new NotificationCompat.Builder(this);
        nb.setSmallIcon(R.drawable.icon);
        nb.setContentTitle("Image Notification");
        nb.setContentText("Set Content text");
        nb.setTicker("Set Ticker text");
        //  nb.setStyle(new NotificationCompat.BigTextStyle().bigText("Big View Styles"));



       /* try 
            URL url = new URL("https://www.gstatic.com/webp/gallery3/1.png");
           image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
         catch (IOException e) 
            e.printStackTrace();
         */

        Glide.
                with(this).
                load(url).
                asBitmap()
                .centerCrop()
                .into(new SimpleTarget<Bitmap>(200,200) 
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) 

                        NotificationCompat.BigPictureStyle bps = new NotificationCompat.BigPictureStyle().bigPicture(resource);
                        bps.setSummaryText("Summary text appears on expanding the notification");
                        nb.setStyle(bps);

                    
                );


        // Width and height


        // NotificationCompat.BigPictureStyle bps = new NotificationCompat.BigPictureStyle().bigPicture(bmp);
        //  bps.setSummaryText("Summary text appears on expanding the notification");
        //  nb.setStyle(bps);





       /* NotificationCompat.BigPictureStyle bps = new NotificationCompat.BigPictureStyle().bigPicture(bmp);
        bps.setSummaryText("Summary text appears on expanding the notification");
        nb.setStyle(bps); */
        // Bitmap bitmap_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.picture);
        //  NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
        // s.setSummaryText("Summary text appears on expanding the notification");
        //  nb.setStyle(s);

        TaskStackBuilder TSB = TaskStackBuilder.create(this);
        TSB.addParentStack(VideoNotification.class);
        // Adds the Intent that starts the Activity to the top of the stack
        TSB.addNextIntent(intent);
        PendingIntent resultPendingIntent =
                TSB.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        nb.setContentIntent(resultPendingIntent);
        nb.setAutoCancel(true);
        nb.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        nb.setSound(alarmSound, AudioManager.STREAM_MUSIC);
        NotificationManager mNotificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(11221, nb.build());
    



【讨论】:

实际上我不想使用外部库...但感谢您的时间@vishnumm93

以上是关于单击推送通知时如何打开活动?的主要内容,如果未能解决你的问题,请参考以下文章

如何在通知选项卡中单击推送通知消息打开特定片段?

单击推送通知时如何在我的颤振应用程序中打开特定页面

单击 Ionic 中的推送通知时如何打开特定页面?

单击推送通知时如何打开应用程序的特定页面

推送通知打开一个新活动

从 android 推送通知单击启动时,意图数据未在活动中更新