简体   繁体   English

如何在单击推送通知时打开特定活动

[英]How to open specific Activity on Click of Push Notification

I am developing an android app in which application i am adding notification service using GCM. 我正在开发一个Android应用,其中我正在使用GCM添加​​通知服务的应用。 The problem is that i want to open specific activity on click of notification. 问题是我想在单击通知时打开特定的活动。 I don't Know how to do this task.I also want to change the changes on server side and client side. 我不知道如何执行此任务。我还想更改服务器端和客户端的更改。 The activity name be provided by the server and then when i click on the notification the activity opens. 活动名称由服务器提供,然后在我单击通知时打开活动。 please help me . 请帮我 。

I've tried this code. 我已经试过了这段代码。

protected void onMessage(Context context, Intent intent) {
        Log.i(TAG,
                "Received message. Extras:*************************************************** "
                        + intent.getExtras());
        // String message = getString(R.string.gcm_message);
        // displayMessage(context, message);
        // notifies user

        String inAction = intent.getAction();
        ArrayList<Notify> notifyData = new ArrayList<Notify>();
        if (inAction.equals("com.google.android.c2dm.intent.RECEIVE")) {

            String json_info = intent.getExtras().getString("data");
            Notify notify = new Notify();
            if (json_info != null) {
                try {

                    JSONObject jsonObj = new JSONObject(json_info);
                    notify.setdetail(jsonObj.get("Detail").toString());
                    notify.setappUpdate(jsonObj.get("App Update").toString());
                    notify.setcontentUpdate(jsonObj.get("Content Update")
                            .toString());
                    notify.setSpecial(jsonObj.get("Special Event").toString());
                    notify.setSpecialContent(jsonObj.get("splEvtDes")
                            .toString());
                    notifyData.add(notify);
                } catch (JSONException e) {
                    // do nothing
                    return;
                }
            } else {

                notify.setdetail(intent.getStringExtra("Detail"));
                notify.setappUpdate(intent.getStringExtra("App Update"));
                notify.setcontentUpdate(intent.getStringExtra("Content Update"));
                notify.setSpecial(intent.getStringExtra("Special Event"));
                notify.setSpecialContent(intent.getStringExtra("splEvtDes"));
                notifyData.add(notify);
            }

        }
        String message = null;
        if (notifyData.get(0).getappUpdate().equalsIgnoreCase("YES")) {
            createNotificationForAppUpdate();
        } else if (notifyData.get(0).getcontentUpdate().equalsIgnoreCase("YES")) {
            message = notifyData.get(0).getdetail();

            generateNotification(context, message);
        } else {
            System.out
                    .println("=-----------------------------inside else_________________________");
            message = notifyData.get(0).getSpecialContent();
            generateNotification(context, message);

        }

    }

According to your example code,seems you are trying to perform different action in case of Appupdate, contentUpdate and Special content.So according to your code create three method.first for AppUpdate,2nd for content Update and last one for specialcontent. 根据您的示例代码,似乎您正在尝试对Appupdate,contentUpdate和Special内容执行不同的操作。因此,根据您的代码创建三个方法。第一个方法用于AppUpdate,第二个用于内容更新,最后一个用于特殊内容。

         String message = null;
                if (notifyData.get(0).getappUpdate().equalsIgnoreCase("YES")) {
                    createNotificationForAppUpdate();
    //This will notify for App update
                } else if (notifyData.get(0).getcontentUpdate().equalsIgnoreCase("YES")) {
                    message = notifyData.get(0).getdetail();
        //This will notify for Content updte
                    generateNotification(context, message);
                } else {

//This will notify for special content
                    message = notifyData.get(0).getSpecialContent();
                    generateNotification(context, message);

                }

Method for AppUpdate AppUpdate方法

private void createNotificationForAppUpdate()
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("Enter here playstore link"));
startActivity(browserIntent);
}

Method for Content Notification. 内容通知的方法。

 private void generateNotification(Context context,String message){
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(context, YourActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
        PendingIntent.getActivity(context, 0, viewIntent, 0);

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_event)
        .setContentTitle("Title")
        .setContentText(message)
        .setContentIntent(viewPendingIntent);

// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(context);

// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
 }

Accordingly create method for special too. 因此也为特殊的创造方法。 Hope this will help GOOD LUCK 希望这会帮助好运

You can specify any activity to be receiver for push notifications: 您可以指定任何活动来接收推送通知:

<intent-filter>
<action android:name="PACKAGE_NAME.MESSAGE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

This intent filter for the activity specifies which activity will be launched in response to push notification (PACKAGE_NAME is your Android app package) 该活动的意图过滤器指定了将响应推送通知而启动的活动(PACKAGE_NAME是您的Android应用包)

So you can add this intent filter in your activity which you want to open on the click of Push notification. 因此,您可以在要通过单击“推送”通知打开的活动中添加此意图过滤器。

Referenec: More Info Referenec: 更多信息

Mention you specif acitivity (here MainActivity.class) in Pending Intent like this 在Pending Intent中像这样指定您的活动性(此处为MainActivity.class)

Intent notificationIntent = new Intent(context, MainActivity.class);

PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
private void notificationMethod(){

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(yourActivity.this)
                .setSmallIcon(R.drawable.app_icon)
                .setContentTitle("title")
                .setContentText("text");
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(yourActivity.this, wantToOpenActivity.this);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(yourActivity.this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack( wantToOpenActivity.this);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        int mId=001;;
        mNotificationManager.notify(mId, mBuilder.build());
    }
<intent-filter>
      <action android:name="android.intent.action.VIEW"/>
     <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

I got similar problem and the activity opens after I include "android.intent.action.VIEW". 我遇到了类似的问题,并且在包含“ android.intent.action.VIEW”之后打开了活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打开一个活动以显示消息推送通知? - how to open an activity to display message push notification? 如何在Android中单击通知打开相同的活动? - How to open same activity on click of notification in android? 推送通知打开一个新活动 - Push notification open a new activity 单击通知选项卡中的推送通知消息,如何打开特定片段? - How to open the particular fragment on the click of the push notification message in the notification tab? 当应用程序无法在后台运行时,如何在点击推送通知中打开网址 - how to open url on click push notification when app is not working in background 如何在点击通知时打开活动 - How can i open Activity when notification click 在不同的Activity Android中解析Open push通知 - Parse Open push notification in different Activity Android 单击推送通知时未打开新活动 - New Activity not opening on push notification click 当我点击通知推送时,特定活动无法打开(需要真正的帮助) - When i push notification clicked, specific activity doesn't open (need really help) Android-为什么在点击通知后“活动”处于打开状态? - Android - Why Activity is Open after click notification?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM