Android – the intention for a specific activity when you click the push notification

When the app is in the background / not running, the specific activity is not opened when the notification is clicked

Notification click will start the specified activity only when the application is opened and notification click is performed. If the application is in the background / not running and notification click is performed, the mainactivity of the application will be opened. In short, it is like that the application opens normally after the activity stack rather than the specified activity in pendingintent

Firebase instance ID service:

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 messaging service:

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

In 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>

resolvent:

Try using remotemessage. Getdata() instead of remotemessage. Getnotification()

Update onmessagereceived() as follows:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>