6

我正在开发需要显示通知的应用程序。 对于通知,我使用FireBase Cloud Messaging (FCM)。 我可以在应用程序处于后台时获取通知。在FCM中点击通知时打开特定活动

但是,当我点击通知,它重定向到home.java页面。我希望它重定向到Notification.java页面。

因此,请告诉我如何在通知单击时指定活动。 我使用两种服务:

1)MyFirebaseMessagingService

2)MyFirebaseInstanceIDService

这是我onMessageReceived(代码示例)在MyFirebaseMessagingService类方法。

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "FirebaseMessageService"; 
Bitmap bitmap; 


public void onMessageReceived(RemoteMessage remoteMessage) { 



    Log.d(TAG, "From: " + remoteMessage.getFrom()); 

    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
    } 

    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) { 
     Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
    } 

    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 
} 
/** 
* Create and show a simple notification containing the received FCM message. 
*/ 

private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) { 
    Intent intent = new Intent(this, Notification.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    intent.putExtra("Notification", 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*/ 
      .setContentTitle(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()); 
} 

/* 
*To get a Bitmap image from the URL received 
* */ 
public Bitmap getBitmapfromUrl(String imageUrl) { 
    try { 
     URL url = new URL(imageUrl); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap bitmap = BitmapFactory.decodeStream(input); 
     return bitmap; 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return null; 

    } 
} 
+0

删除 “通知”,也将努力 –

+0

感谢:

public void onMessageReceived(RemoteMessage remoteMessage){ String title=remoteMessage.getNotification().getTitle(); String message=remoteMessage.getNotification().getBody(); String click_action=remoteMessage.getNotification().getClickAction(); Intent intent=new Intent(click_action); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT); NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this); notificationBuilder.setContentTitle(title); notificationBuilder.setContentText(message); notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); notificationBuilder.setAutoCancel(true); notificationBuilder.setContentIntent(pendingIntent); NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0,notificationBuilder.build()); } 

您的通知云功能/服务器代码期待你的答复! –

+0

你的意思是说,从getNotification()中移除getNotification()。getBody(); ?? –

回答

0

当应用程序在后台意图必须在发射活动。所以它会打开你的启动activity.Now你检查是否有在意向书中发射活动然后启动所需的活动数据传递。

10

根据FCM文件Receive and handle notifications

  • 通知时,您的应用程序在后台交付。在这种情况下,通知将传递到设备的系统托盘。 默认情况下,用户点击通知将打开应用程序启动器。
  • 带有通知和数据有效负载的消息,包括背景和前景。在这种情况下,通知将传递到 设备的系统托盘,并且数据有效负载将通过您的启动器活动意图的附加组件 来传递。

手柄通知消息在背景执行的应用程序:

当你的应用程序是在后台,Android的指示通知消息到系统托盘。用户点击通知会默认打开应用程序启动器。

这包括包含通知和数据负载(以及通知控制台发送的所有消息)的消息。在这些情况下,通知将传递到设备的系统托盘,并且数据有效载荷将按照启动程序活动的目的附加内容进行传递。

随着FCM,您可以发送两种类型的消息给客户:

1.通知消息:有时认为是“显示的消息。”

FCM代表客户端应用程序自动向最终用户设备显示消息。通知消息具有一组预定义的用户可见密钥。

2.数据信息:由客户端应用程序处理。

客户端应用程序负责处理数据消息。数据消息只有自定义的键值对。

结帐这里现在越来越About FCM Messages

设置click_action通知有效载荷:

所以,如果你要处理的消息在后台来了,你必须发送click_action与消息。 click_actionparameter of the notification payload

如果你要打开你的应用程序,并执行特定操作,在通知有效载荷设置click_action并将其映射到您要启动活动的意图过滤器。例如,设置到click_actionOPEN_ACTIVITY_1触发等的意图过滤器执行以下操作:

<intent-filter> 
    <action android:name="OPEN_ACTIVITY_1" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

FCM有效载荷看起来像下面:

{ 
    "to":"some_device_token", 
    "content_available": true, 
    "notification": { 
     "title": "hello", 
     "body": "test message", 
     "click_action": "OPEN_ACTIVITY_1" 
    }, 
    "data": { 
     "extra":"juice" 
    } 
} 
+3

很好地复制粘贴,但这是什么答案? –

+0

@TimCastelijns检查我的更新答案。 –

0

的AndroidManifest.xml

<activity android:name="YOUR_ACTIVITY"> 
    <intent-filter> 
     <action android:name="com.example.yourapplication_YOUR_NOTIFICATION_NAME" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

您[R FirebaseMessagingService.java文件onMessageReceived方法:从您的FCM有效载荷

notification: { 
     title: "TITLE OF NOTIFICATION", 
     body: "NOTIFICATION MESSAGE", 
     sound: "default", 
     click_action: "com.example.myapplication_YOUR_NOTIFICATION_NAME" 
    }