【问题标题】:Notification does not correctly work (Android)通知无法正常工作 (Android)
【发布时间】:2016-04-05 13:11:09
【问题描述】:

我的通知无法正常工作。当我的应用程序打开并收到通知时 -> 我单击并进入我需要的活动。但是当我的应用程序关闭并收到通知时->我单击此通知->转到家庭活动,而不是我需要的活动。

例如我有HomeActivityNotificationActivity。当我的应用程序打开并收到通知时,我会转到NotificationActivity,但是当我的应用程序关闭并单击时,我会转到HomeActivity(我不需要去这个地方)。

count++;
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
                PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
                wl.acquire(15000);

                NotificationCompat.Builder mBuilder;
                mBuilder = new NotificationCompat.Builder(ctx);

                mBuilder.setSmallIcon(R.drawable.notification); 
                mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
mBuilder.setVibrate(new long[]{1000, 1000});
                mBuilder.setTicker(name);
                mBuilder.setContentText(message);
                mBuilder.setAutoCancel(true);
                mBuilder.setDefaults(Notification.DEFAULT_ALL);
                mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));

Intent i = new Intent(ctx, NotificationActivity.class); 
 i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

 PendingIntent resultPendingIntent =
                        PendingIntent.getActivity(
                                ctx,
                                count,
                                i,
                                PendingIntent.FLAG_UPDATE_CURRENT);


               mBuilder.setContentIntent(resultPendingIntent);
               NotificationManager myNotificationManager = (NotificationManager)
                       ctx.getSystemService(ctx.NOTIFICATION_SERVICE);

                myNotificationManager.notify(count, mBuilder.build());

【问题讨论】:

    标签: android android-intent notifications


    【解决方案1】:

    在推送通知的服务器端的通知对象中添加 click_action 像 click_action = 'notification_activity' 你的 fcm 像这样发送通知服务器端对象

    {
     "to" : "YOUR_FCM_TOKEN_WILL_BE_HERE",
     "collapse_key" : "type_a",
     "notification" : {
         "body" : "First Notification",
         "title": "Collapsing A",
         "click_action": "notification_activity"
     }
    }
    

    在此之后,您必须在 manufeast 文件中为 Notification Activity 添加意图过滤器

    <activity android:name=".NotificationActivity"
        <intent-filter>
            <action android:name="notification_activity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    

    在此之后,您的 firebase 消息传递服务更改如下

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
    
            String action = remoteMessage.getNotification().getClickAction();
            sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(),action);
        }
        private void sendNotification(String body, String title, String action) {
            Intent intent = new Intent(action);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent,PendingIntent.FLAG_ONE_SHOT);
    //here your notification builder code...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 2019-11-03
      • 2016-11-27
      相关资源
      最近更新 更多