在Android中,FCM通知点击后如何打开活动

3

我知道有很多类似的帖子,但我尝试了许多解决方案,但没有任何一个适用于我。

我正在使用Firebase Cloud Messaging发送通知。 通知已经到达,但它打开的是主活动,而不是我想要的活动。

我尝试过很多次,但都不能启动适当的活动。 尝试了不同的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。 - Murli Prajapati
请参考此链接:https://dev59.com/s1oU5IYBdhLWcg3wV14Z - rafsanahmad007
代码看起来还不错。你能发一下你的服务器端代码或者一个样例负载吗? - AL.
sendNotification()在if之外没有帮助,我添加了日志,这些日志在点击通知后显示。我没有其他带有“Firebase”的日志,因此似乎它甚至没有进入我的FirebaseMessagingService(或者只是不在logcat中显示日志)。 - Piotr Sagalara
1个回答

5
如果应用程序在后台,通知将直接指向系统托盘。
点击通知后,它会重定向到清单文件中给出的启动器活动。
从启动器活动中,我们可以调用任何活动,但首先它会转到启动器活动。
在启动器活动中,我们可以获取通知消息内容。
AndroidManifest.xml
 <activity

            android:name=".view.SplashActivity"

            android:screenOrientation="portrait">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />



                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

SplashActivity.Java

public class SplashActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        Bundle bundle = getIntent().getExtras();

        if (bundle != null && bundle.get("data")!=null) {


    //here can get notification message
          String datas = bundle.get("data").toString();

    }
}
}

1
这里的问题和答案有什么关系? - Zahan Safallwa
在我的情况下,我想打开启动器活动,但当我点击通知时什么也没发生。 - Siddhpura Amit
你可以通过“数据”来指导你的应用程序。 - Ucdemir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接