【问题标题】:How to open a specific activity on push notification tapped?如何在点击推送通知时打开特定活动?
【发布时间】:2017-09-15 16:56:45
【问题描述】:

好的,我现在正在做的是通过 FCM 收到推送通知,进展顺利。现在我可以在应用程序处于前台时更改活动,但是当我点击通知面板中的通知时如何更改它?需要帮助。

我的代码:

public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {

        // Check for empty push message
        if (TextUtils.isEmpty(message))
            return;

        // notification icon
        final int icon = R.mipmap.ic_launcher;

        // on click activity for the notification !!!!!!!!!!
        intent = new Intent(Intent.ACTION_MAIN);
        intent.setClass(mContext, TestActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        final PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        mContext,
                        0,
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );

        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                mContext);

【问题讨论】:

    标签: android firebase push-notification android-notifications firebase-cloud-messaging


    【解决方案1】:

    在点击 Intent 时传递您想要打开的 Activity。

    Intent notificationIntent = new Intent(context, XYZActivity.class);
    

    完整代码:

    NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(icon, message, when);
        
            Intent notificationIntent = new Intent(context, XYZActivity.class);
        
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        
            PendingIntent intent = PendingIntent.getActivity(context, 0,
                    notificationIntent, 0);
        
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0, notification);
    

    【讨论】:

    • 我做了同样的事情,但它仍然不断打开主要活动。
    【解决方案2】:
            NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    
            Intent notificationIntent = new Intent(mContext,ACTIVITY_TO_BE_DISPLAYED.class); // Replace ACTIVITY_TO_BE_DISPLAYED to Activity to which you wanna show
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
            PendingIntent intent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                    .setAutoCancel(true)
                    .setTicker("YOUR_TICKER_MSG")
                    .setSmallIcon(R.drawable.ic_notification_icon)
                    .setLargeIcon(icon)
                    .setContentTitle("YOUR_TITLE")
                    .setContentText("YOUR_TEXT")
                    .setContentIntent(intent);
            notificationManager.notify(10, builder.build());
    

    【讨论】:

      【解决方案3】:
      private void sendNotification(String msg) {
          mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          long notificatioId = System.currentTimeMillis();
      
          Intent intent = new Intent(getApplicationContext(), TestActivity.class); // Here pass your activity where you want to redirect.
      
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
          PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, 0);
      
          int currentapiVersion = android.os.Build.VERSION.SDK_INT;
          if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
              currentapiVersion = R.mipmap.ic_notification_lolipop;
          } else{
              currentapiVersion = R.mipmap.ic_launcher;
          }
      
          NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                  .setSmallIcon(currentapiVersion)
                  .setContentTitle(this.getResources().getString(R.string.app_name))
                  .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                  .setContentText(msg)
                  .setAutoCancel(true)
                  .setPriority(Notification.PRIORITY_HIGH)
                  .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                  .setContentIntent(contentIntent);
          mNotificationManager.notify((int) notificatioId, notificationBuilder.build());
      }
      

      【讨论】:

      • 感谢您的回答。对弄清楚有很大帮助。
      【解决方案4】:

      我已经用这种方式开始了一个特定的活动:

      FireBaseMessagingService.java

          @Override
          public void onMessageReceived(RemoteMessage remoteMessage) {
              //The message will contain the Push Message
              String message = remoteMessage.getData().get("message");
              //imageUri will contain URL of the image to be displayed with Notification
              String imageUri = remoteMessage.getData().get("image");
              //title for the notification.
              String title = remoteMessage.getData().get("title");
              //action string to perform the action e.g. open activity
              String action = remoteMessage.getData().get("click_action");
              //To get a Bitmap image from the URL received
              bitmap = getBitmapfromUrl(imageUri);
              //method for functioning the notification --->
              sendNotification(message, title, bitmap, action);
          }
      
       private void sendNotification(String messageBody, String title, Bitmap image, String action) {
                  Intent intent = new Intent(this, SpecificActivity.class);
                  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  intent.putExtra("title", title);
                  ByteArrayOutputStream _bs = new ByteArrayOutputStream();
                  image.compress(Bitmap.CompressFormat.PNG, 50, _bs);
                  intent.putExtra("img", image);
                  intent.putExtra("msg", messageBody);
                  intent.putExtra("click_action", action);
                  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, "Default")
                          .setLargeIcon(image)/*Notification icon image*/
                          .setSmallIcon(R.mipmap.app_icon)
                          .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))/*Notification with Image*/
                          .setAutoCancel(true)
                          .setSound(defaultSoundUri)
                          .setPriority(Notification.PRIORITY_HIGH)
                          .setChannelId("Default")
                          .setVibrate(new long[]{1000, 1000})
                          .setContentIntent(pendingIntent);
      
                  notificationBuilder.setContentTitle(title);
                  notificationBuilder.setContentText(messageBody);
                  notificationBuilder.setAutoCancel(true);
              NotificationManager notificationManager =
                      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
              notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
      
          }
      

      之后在 AndroidManifest.xml 文件的 specificActivity.java 部分添加以下行:

       <activity
                  android:name=".SpecificActivity">
                  <intent-filter>
                      <action android:name="OPEN_ACTIVITY" />
       <!-- Add this OPEN_ACTIVITY string into your data payload while sending the notification from server side. -->
                      <category android:name="android.intent.category.DEFAULT" />
                  </intent-filter>
              </activity>
      

      在此之后获取您正在启动的特定活动的意图,即 SpecificActivity.java 文件的 onCreate() 方法。

      if (getIntent().getExtras() != null) {
                  for (String key : getIntent().getExtras().keySet()) 
                      {
                      String value = getIntent().getExtras().getString(key);
                      if (key.equals("click_action")) {
                      //perform the action you want to do with the key.
                      }
      

      添加这些后,您就可以查看来自移动端的通知了。

      【讨论】:

        【解决方案5】:
        <!-- MainActivity is the parent for ResultActivity -->
            <activity
                android:name=".ResultActivity"
               />
        

        不要忘记使用子活动声明来调整清单

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多