Pio*_*ara 3 android push-notification firebase firebase-cloud-messaging
我知道有很多这样的帖子,但我尝试了很多解决方案,但对我来说没有任何效果。
我正在使用 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>
Run Code Online (Sandbox Code Playgroud)
Firebase 消息服务:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
点击后的日志:
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) }
Run Code Online (Sandbox Code Playgroud)
如果应用程序在后台,通知将定向到系统托盘。
单击通知后,它将重定向到清单文件中给出的启动器活动。
从启动器活动我们可以调用任何活动,但首先它会转到启动器活动。
在 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>
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9995 次 |
| 最近记录: |