1

实际上,我正在实施firebasePushNotification没有任何问题的服务。现在,当我收到push_notification并且我的应用程序通过单击通知意图处于前台时,正在将其传递给所需的活动,而不会出现任何错误。但是,当应用程序处于后台或未打开时,单击通知不会将意图发送到所需的活动,甚至没有显示任何错误。我试图在SO中提到一些关于在其中添加exported=true选项的问题,manifest.xml但它无法解决我的问题。

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());
    }
}
4

2 回答 2

2

如果您正在使用firebase push notification

您需要发送一个click_action属性...以在应用程序处于后台时打开所需的活动。

所以试试..

 Intent i = new Intent(click_action);

click_actionintent-filter到所需活动的映射。click_action 是强制性的

而不是显式意图调用- 上述情况下的显式意图调用在应用程序处于前台时才有效..

于 2018-04-17T19:05:04.403 回答
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"/>

消息服务

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());
}

}

于 2018-04-17T19:01:54.107 回答