【发布时间】:2017-04-06 19:02:49
【问题描述】:
我知道有很多这样的帖子,但我尝试了很多解决方案,但没有任何适合我的方法。
我正在使用 Firebase 云消息传递来发送通知。通知到达,但它打开了主要活动,而不是我想要的活动。
我一直在尝试,但在开始适当的活动时总是失败。尝试了不同的 Intent 或 PendingIntent 设置,尝试了 click_action,但对我没有任何作用。
我认为它甚至没有进入我的 FirebaseMessagingService,但我按照 Firebase 指令执行了所有操作。
清单:
<service
android:name="com.packagename.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name="com.packagename.NotificationActivity">
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</activity>
FirebaseMessagingService:
public class FirebaseMessagingService extends FirebaseMessagingService {
private static String TAG = "Firebase";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if(remoteMessage.getNotification()!=null){
Log.d(TAG,"Message body : "+remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
}
}
private void sendNotification(String body, String title) {
Intent notificationIntent = new Intent(this, NotificationActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("notification", body);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notification =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification)
.setContentTitle(getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body)
.setBigContentTitle(title))
.setContentText(body)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_SOUND);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notification.build());
}
}
点击后的日志:
D/StatusBar: Clicked on content of 0|com.mypackagename|0|GCM-Notification:89893881|10177 , PendingIntent{cc15013: android.os.BinderProxy@e3031fc} , Intent { act=com.google.firebase.MESSAGING_EVENT cmp=com.mypackagename/com.google.firebase.iid.FirebaseInstanceIdInternalReceiver (has extras) }
【问题讨论】:
-
尝试在 if 条件之外调用 sendNotification() 方法,因为如果应用在后台,getNotification() 可能会返回 null。
-
代码对我来说看起来不错。您能否发布您的服务器端代码或示例有效负载?
-
sendNotification() 外部如果没有帮助,我添加了单击通知后显示的日志。我没有任何其他带有“Firebase”的日志,所以它似乎甚至没有进入我的 FirebaseMessagingService(或者只是没有在 logcat 中显示日志)。
标签: android firebase push-notification firebase-cloud-messaging