后台服务发送通知,当用户点击通知时,它必须启动相应的活动

时间:2017-12-24 07:46:51

标签: android

我正在开发一个小应用,我希望后台服务发送通知,当用户点击通知时,它必须启动相应的活动。我在这里发布通知的代码,我的问题是通知显示在状态栏上,但是当我点击它时它不会启动所需的活动(而是启动主要活动)。有人可以建议我哪里出错了。请帮忙。

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

以下是我的清单文件

<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>

1 个答案:

答案 0 :(得分:1)

这一行有问题。

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

它应该是。

DECLARE  @MyTable TABLE(debit INT, credit INT,  code INT)

INSERT INTO @MyTable VALUES 
(0, 10, 5),
(5, 0 , 3),
(0, 11, 2),
(0, 15, 1),
(7, 0 , 6),
(6, 0 , 2),
(5, 0 , 1)

SELECT * FROM 
    @MyTable 
ORDER BY 
    (CASE WHEN debit > 0 THEN 0 ELSE 1 END) ,
    code , 
    debit

原因意图是调用活动而不是服务。