chitraksh bairathee
chitraksh bairathee

Reputation: 123

background service to send a notification and when the user clicks the notification it must launch the appropriate activity

I am developing a small app in which I want a background service to send a notification and when the user clicks the notification it must launch the appropriate activity. I am posting the code here for the notification and my problem is that the notification gets displayed on the status bar but when i click it it does not launch the required activity(instead it is launching the mainactivity). Can somebody suggest where I am going wrong. Please help.

    Intent intent = new Intent(this, IncidentDetail.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getService(this,0  /* Request code */
            , intent,PendingIntent.FLAG_UPDATE_CURRENT);
 //here i tried with both getService and getActivity
  /*PendingIntent pendingIntent = PendingIntent.getActivity(this,0 
            , intent,PendingIntent.FLAG_UPDATE_CURRENT);*/

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(Title)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent)
                    ;

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

Below is my manifest file

<service android:name=".MyFirebaseMessagingService" >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <activity
        android:name=".IncidentDetail"
        android:label="@string/title_activity_incident_detail"
        android:exported="true"
        android:launchMode="singleTop">
    </activity>

Upvotes: 2

Views: 39

Answers (1)

SpiritCrusher
SpiritCrusher

Reputation: 21043

Problem in this line.

PendingIntent pendingIntent = PendingIntent.getService(this,0  /* Request code */
        , intent,PendingIntent.FLAG_UPDATE_CURRENT);

It shoule be.

PendingIntent pendingIntent = PendingIntent.getActivity(this,0  /* Request code */
        , intent,PendingIntent.FLAG_UPDATE_CURRENT);

Cause Intent is calling an activity not a service .

Upvotes: 1

Related Questions