简体   繁体   English

点击推送通知时意图进行特定活动。

[英]intent to specific activity when push notification clicked in.

Not opening specific activity on notification click when the app is in background/not running 当应用处于后台/未运行时,未在通知单击时打开特定活动

The notification-click starts specified activity only when the app is opened up and the notification-click is performed. 只有在打开应用程序并执行通知单击时,通知单击才会启动指定的活动。 If the app is in background/not running and the notification-click is performed, the application's MainActivity opens up. 如果应用程序处于后台/未运行且执行了通知单击,则会打开应用程序的MainActivity。 In short, it is like the app opens normally following the activity stack instead of opening the specified activity in the PendingIntent. 简而言之,就像应用程序在活动堆栈之后正常打开而不是在PendingIntent中打开指定的活动。

Firebase Instance Id Service: Firebase实例ID服务:

package com.example.tamzid.pushnotification;

 import android.util.Log;
 import com.google.firebase.iid.FirebaseInstanceId;
 import com.google.firebase.iid.FirebaseInstanceIdService;

 public class MyAndroidFirebaseInstanceIdService extends 
FirebaseInstanceIdService {

private static final String TAG = "MyAndroidFCMIIDService";

@Override
public void onTokenRefresh() {
    //Get hold of the registration token
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    //Log the token
    Log.d(TAG, "Refreshed token: " + refreshedToken);
}
private void sendRegistrationToServer(String token) {
    //Implement this method if you want to store the token on your server
}
}

Firebase Message Service: Firebase消息服务:

 package com.example.tamzid.pushnotification;

 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.media.RingtoneManager;
 import android.net.Uri;
 import android.support.v4.app.NotificationCompat;
 import android.util.Log;
 import com.google.firebase.messaging.FirebaseMessagingService;
 import com.google.firebase.messaging.RemoteMessage;

 public class MyAndroidFirebaseMsgService extends FirebaseMessagingService     
{
private static final String TAG = "MyAndroidFCMService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Log data to Log Cat
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " +     
remoteMessage.getNotification().getBody());
    //create notification
    createNotification(remoteMessage.getNotification().getBody());
}

private void createNotification( String messageBody) {
    Intent intent = new Intent( this , ResultActivity. class );
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent resultIntent = PendingIntent.getActivity( this , 0, 
intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri notificationSoundURI = 
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mNotificationBuilder = new 
NotificationCompat.Builder( this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Android Tutorial Point FCM Tutorial")
            .setContentText(messageBody)
            .setAutoCancel( true )
            .setSound(notificationSoundURI)
            .setContentIntent(resultIntent);

    NotificationManager notificationManager =
            (NotificationManager)  
getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, mNotificationBuilder.build());
 }
}

AndroidManifest.xml AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tamzid.pushnotification">

<application
    android:name="android.support.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ResultActivity"></activity>
    <service android:name=".MyAndroidFirebaseMsgService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <service android:name=".MyAndroidFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" 
/>
        </intent-filter>
    </service>

</application>

</manifest>

Try using remoteMessage.getData() instead of remoteMessage.getNotification() 尝试使用remoteMessage.getData()而不是remoteMessage.getNotification()

Use remoteMessage.getNotification() : if message contains a notification payload. 使用remoteMessage.getNotification() :if message包含通知有效负载。

Use remoteMessage.getData() :if message contains a data payload. 使用remoteMessage.getData() :if message包含数据有效负载。

Update onMessageReceived() as below: 更新onMessageReceived()如下:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) 
    {
        //create notification
        createNotification(remoteMessage.getData().toString());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击推送通知时,特定活动不会打开 - when push notification clicked, specific activity doesn't open 单击推送通知时的活动(Android) - Activity when push notification is clicked (Android) 在后台单击通知时打开特定活动 - Open specific activity when notification clicked in background 在 FCM 中单击通知时打开特定活动 - Open specific Activity when notification clicked in FCM 当我点击通知推送时,特定活动无法打开(需要真正的帮助) - When i push notification clicked, specific activity doesn't open (need really help) 推送通知-单击通知时获取意图信息,应用正在运行 - Push notifications - getting the intent information when notification clicked, app running 单击通知时检查活动是否在屏幕上?(使用意图) - Check For Activity is onScreen when Notification is Clicked ?(using Intent) 单击推送通知时如何在 WebView 中加载特定的 URL 页面 - How to load Specific URL page in WebView when the Push Notification is clicked 推送通知始终启动主要活动意图 - Push Notification always Start Main Activity Intent 推送通知打开具有相同意图的活动 - Push notification open Activity with same intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM