【问题标题】:Open activity after FCM notification click in Android在Android中单击FCM通知后打开活动
【发布时间】:2017-04-06 19:02:49
【问题描述】:

我知道有很多这样的帖子,但我尝试了很多解决方案,但没有任何适合我的方法。

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

我一直在尝试,但在开始适当的活动时总是失败。尝试了不同的 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) }

【问题讨论】:

  • 尝试在 if 条件之外调用 sendNotification() 方法,因为如果应用在后台,getNotification() 可能会返回 null。
  • 代码对我来说看起来不错。您能否发布您的服务器端代码或示例有效负载?
  • sendNotification() 外部如果没有帮助,我添加了单击通知后显示的日志。我没有任何其他带有“Firebase”的日志,所以它似乎甚至没有进入我的 FirebaseMessagingService(或者只是没有在 logcat 中显示日志)。

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

如果应用程序在后台,通知会定向到系统托盘。

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

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

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

    }
}
}

【讨论】:

  • 这里的问答关系是什么??
  • 在我的情况下,我想打开启动器活动,但是当我点击通知时没有任何事情发生
  • 你可以用“数据”来指导你的应用
猜你喜欢
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 2011-11-07
  • 1970-01-01
相关资源
最近更新 更多