首页 文章

在FCM通知点击Android后打开活动

提问于
浏览
2

我知道有很多这样的帖子,但我尝试了很多解决方案,对我来说没什么用 .

我正在使用Firebase Cloud 消息传递来发送通知 . 通知到达,但它会打开主要活动,而不是我想要的活动 .

我尝试尝试,但继续开始适当的活动失败 . 尝试了不同的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) }

1 回答

  • 3

    如果应用程序在后台,通知将指向系统托盘 .

    点击通知后,它将重定向到清单文件中给出的启动器活动 .

    从启动器活动我们可以调用任何活动,但首先它将转到启动器活动 .

    在Launcher活动中,我们可以获得通知消息内容 .

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

相关问题