单击推送通知时对特定活动的意图。
Posted
技术标签:
【中文标题】单击推送通知时对特定活动的意图。【英文标题】:intent to specific activity when push notification clicked in. 【发布时间】:2017-04-13 10:13:41 【问题描述】:当应用程序处于后台/未运行时,未在通知点击时打开特定活动
仅当应用程序打开并执行通知单击时,通知单击才会启动指定的活动。如果应用程序处于后台/未运行并且执行了通知单击,则应用程序的 MainActivity 将打开。简而言之,就像应用正常打开是跟随activity栈而不是打开PendingIntent中的指定activity一样。
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 消息服务:
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
<?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>
【问题讨论】:
您使用了 getNotification() ,它仅在应用程序处于前台时才有效。你需要使用 getData() 来做到这一点 当应用程序在后台时,您的 public void onMessageReceived(RemoteMessage remoteMessage) 永远不会被调用。系统句柄通知,因此您不能在那里指定自定义活动。如果你使用 Data() 那么你可以做任何你想做的事情 好的,先生...... 【参考方案1】:尝试使用remoteMessage.getData()
而不是remoteMessage.getNotification()
使用
remoteMessage.getNotification()
:如果消息包含 通知有效载荷。使用
remoteMessage.getData()
:如果消息包含 数据负载。
更新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());
【讨论】:
以上是关于单击推送通知时对特定活动的意图。的主要内容,如果未能解决你的问题,请参考以下文章