Soru & Cevap

Notification ile farklı bir activity açma ...

01.12.2020 - 06:59

Arkadaşlar Firebase Cloud Message'ı kullanmaya çalışıyorum. Asıl amacım cloud message ile gönderilen Key/Value 'leri alıp bu değerlere göre farklı aktiviteleri açmak. Bildirim telefona geliyor, değerleri kaydedebiliyorum ve basınca ana aktivite açılıyor. Fakat tanımladığım PendingIntent bir türlü çalışmadı.

Manifest

<application
        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/Theme.Notifications">
        <activity android:name=".SecondActivity"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyFirebaseMessagingService"
            android:directBootAware="true"
            android:exported="false"
            tools:targetApi="n">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>

 

FirebaseMessageService

@SuppressLint("MissingFirebaseInstanceTokenRefresh")
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    

        RemoteMessage.Notification getNotification = remoteMessage.getNotification();

        assert getNotification != null;

        String CHANNEL_ID = "MYCHANNEL";
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "name", NotificationManager.IMPORTANCE_LOW);

        int requestID = (int) System.currentTimeMillis();

        Intent intent = new Intent(this, SecondActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID)
                .setContentText(getNotification.getBody())
                .setContentTitle(getNotification.getTitle())
                .setContentIntent(pendingIntent)
                .setChannelId(CHANNEL_ID)
                .setSmallIcon(android.R.drawable.sym_action_chat)
                .build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
        notificationManager.notify(1, notification);
    }
}

 

Tam olarak kavrayamadığım nokta şu. aynı kodları bir butona atadığımda manuel bildirim oluşturuyorum ve buradan pendingintenti çalıştırabiliyorum.

55 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user hyqhyq
hyqhyq
05.12.2020 - 12:37

Sorun SecondActivity'yi çalıştıramamak mı? Tam anlayadım.

Profile picture for user hyqhyq
hyqhyq
04.12.2020 - 08:17

Şurada bu konu ile ilgili bir makale var.

https://developer.android.com/training/notify-user/navigation