首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在点击推送通知时打开活动?

如何在点击推送通知时打开活动?
EN

Stack Overflow用户
提问于 2016-08-31 14:50:31
回答 4查看 692关注 0票数 2

目前,我正在我的项目中处理推送通知。当推送通知显示时,我想在单击通知时打开我的通知活动,但它总是打开MainActivity为什么?我也用谷歌搜索了一下,但是找不到正确的答案。

这是我的MyFirebaseMessagingService:

代码语言:javascript
复制
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification(remoteMessage.getNotification().getBody());

    }

    private void sendNotification(String messageBody) {


        Intent intent = new Intent(this, MyNotification.class);
        intent.putExtra("Message",messageBody);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                0);

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

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

        notificationManager.notify(0, notificationBuilder.build());
    }
}

下面是我的清单文件:

代码语言:javascript
复制
 <activity android:name=".MyNotification"
            android:parentActivityName=".MainActivity">
            <intent-filter>
                <action android:name=".MyNotification" android:label="@string/app_name"/>

                <category android:name="android.intent.category.DEFAULT"/>


            </intent-filter>

        </activity>

如何在点击哦推送通知时打开我的通知活动。谢谢你的时间..。

EN

回答 4

Stack Overflow用户

发布于 2016-08-31 17:03:52

AFAIK,这段代码在应用程序处于前台时运行。当它在后台时,Firebase会将通知传送到系统托盘。我也遇到过类似的问题。当它传送到系统托盘时,它总是打开MainActivity。我注意到firebase允许将参数传递给Intent。我利用这种技术来读取自定义字符串(从firebase消息控制台打开高级选项,并将键指定为'item‘,将值指定为'MyNotification')。从主活动中读取此字符串,并将控件从MainActivity::onCreate()方法重定向到适当的活动

代码语言:javascript
复制
//private String ITEM = "item";
@Override
protected void onCreate(Bundle savedInstanceState) {
    //Parse the input passed to the activity
    Intent intent = getIntent();
    String input = intent.getStringExtra(ITEM);
    //Redirect to MyNotification
    Intent redirect = new Intent(this, MyNotification.class);
    startActivity(redirect);
}
票数 2
EN

Stack Overflow用户

发布于 2016-08-31 14:59:28

更新此内容:

代码语言:javascript
复制
 private void sendNotification(String messageBody) {


    Intent intent = new Intent(this, MyNotification.class);
    intent.putExtra("Message",messageBody);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MyNotification.class), 0);

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

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

    notificationBuilder.setContentIntent(contentIntent);

    notificationManager.notify(0, notificationBuilder.build());
}
票数 1
EN

Stack Overflow用户

发布于 2016-08-31 15:03:37

尝试在清单中删除此行

代码语言:javascript
复制
android:parentActivityName=".MainActivity"

编辑1:你可以在这里阅读更多https://developer.android.com/training/implementing-navigation/ancestral.html

原因是当您调用"MyNotification“活动时,back堆栈将”自动“将其父活动添加到back堆栈中,因此您可以按back返回到您的"MainActivity”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39242612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档