实际上我正在实施firebasePushNotification
服务,该服务无任何问题。现在,当我收到push_notification
并且我的应用程序处于前台时,通过单击通知意图将被传递到所需的活动而没有任何错误。但是当应用程序处于后台或未打开时,单击通知并不会将意图发送到所需的活动,即使它没有显示任何错误。我试图在 SO 中提及有关在manifest.xml
中添加 exported = true 选项的一些问题,但它无法解决我的问题。
NotificationManager类代码:
public void displayNotification(String title, String body,String post_owner_username,String post_owner_time,
String notif_commenter_username,String notif_comment_time,String follower_username,String type) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx, Constants.CHANNEL_ID)
.setSmallIcon(R.drawable.new_icon)
.setContentTitle(title)
.setContentText(body);
Intent i = new Intent(ctx, ProfileHolder.class);
if (type.equals("likes")) {
Bundle b = new Bundle();
b.putString("post_owner_username", post_owner_username);
b.putString("post_owner_time", post_owner_time);
b.putString("what", "show_notification_post");
i.putExtras(b);
i.putExtra("Open", "starred");
}
else if (type.equals("comments")) {
Bundle b = new Bundle();
b.putString("post_owner_username",post_owner_username);
b.putString("post_owner_time", post_owner_time);
b.putString("notif_commenter_username", notif_commenter_username);
b.putString("notif_comment_time",notif_comment_time);
b.putString("what", "show_notification_post");
i.putExtras(b);
i.putExtra("Open", "starred");
}
else if (type.equals("comment_likes")) {
Bundle b = new Bundle();
b.putString("post_owner_username", post_owner_username);
b.putString("post_owner_time", post_owner_time);
b.putString("notif_commenter_username", notif_commenter_username);
b.putString("notif_comment_time", notif_comment_time);
b.putString("what", "show_notification_post");
i.putExtras(b);
i.putExtra("Open", "starred");
}
else if (type.equals("follow")||type.equals("follow_request")) {
Bundle b = new Bundle();
b.putString("searchUsername", follower_username);
i.putExtras(b);
i.putExtra("Open", "search_profile");
}
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, i, PendingIntent.FLAG_ONE_SHOT);
/*
* Setting the pending intent to notification builder
* */
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr =
(NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
if (mNotifyMgr != null) {
mBuilder.setAutoCancel(true);
mNotifyMgr.notify(1, mBuilder.build());
}
}
答案 0 :(得分:1)
如果您使用firebase push notification
当应用在后台时,您需要发送
click_action
属性...以打开所需的活动。
所以试试..
Intent i = new Intent(click_action);
click_action
是指向所需活动的intent-filter
映射。 click_action是强制性的
而不是显式意图调用 - 在上述情况下显式意图调用只有 才能在应用处于前台时工作..
答案 1 :(得分:0)
以下是我在应用程序处于后台/关闭时通过通知设置打开特定活动并通过Intent
传递数据的方式。
<强>应用强>
public class AppConfig extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
}
}
<强>清单强>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<!-- Don't forget to set exported attribute to true -->
<activity android:name=".MainActivity" android:exported="true"/>
<强>的MessagingService 强>
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String body = null;
if(remoteMessage.getNotification() != null) {
body = remoteMessage.getNotification().getBody();
}
String id = null;
// get data that server passed (if it passed any at all)
if(remoteMessage.getData().size() > 0) {
id = remoteMessage.getData().get("id");
}
sendNotification(body, id);
}
private void sendNotification(String messageBody, String id) {
// put data you want to send through intent
Bundle bundle = new Bundle();
bundle.putString("id", id);
// create intent, put data, and
Intent intent = new Intent(this, ActivityLogin.class);
intent.putExtras(bundle);
// IMPORTANT! clearing previous tasks so desired activity will launch
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
// customising the notification
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}