簡體   English   中英

單擊GCM通知時打開活動

[英]Open activity when GCM notification was clicked

我正在申請gcm,現在我可以收到通知了

但是,當我單擊通知時,它只是打開了應用程序。

我需要打開另一個活動而不是Mainactivity

有什么辦法嗎?

final Intent intent = new Intent(context, YourActivity.class);
intent.putExtra("key", "value");
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.set...;
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());

請閱讀此內容,以獲取有關創建通知的詳細信息http://developer.android.com/guide/topics/ui/notifiers/notifications.html
取決於http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse ,從通知中啟動Activity有兩種一般情況-常規活動和特殊活動。
下面的方法是Activity的開始2類型的示例:

定期活動

    private static final int NOTIFICATION_ID = 602;
    private static final int REQUEST_CODE_START_ACTIVITY = 610;
    /**
     * Create and show a simple notification containing the received GCM message.
     */
    private void sendNotification(String title, String message, Intent intent) {
        // Create a start PendingIntent
        PendingIntent resultPendingIntent = null;
        ComponentName componentName = intent.getComponent();
        if (componentName != null) {
            // 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(this);
            // Adds the back stack for the Intent (but not the Intent itself) <== This comment must be wrong!
            stackBuilder.addParentStack(componentName);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(intent);

            resultPendingIntent = stackBuilder.getPendingIntent(REQUEST_CODE_START_ACTIVITY, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
        } else {
            resultPendingIntent = PendingIntent.getActivity(this, REQUEST_CODE_START_ACTIVITY, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
        }


        // Notification properties
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setContentIntent(resultPendingIntent);

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

        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

    }

用法:

    Intent intent = new Intent(this, SignInActivity_.class);
    sendNotification(TextUtils.isEmpty(title) ? getString(R.string.app_name) : title, message, intent);

清單XML應該如下所示

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".SignInActivity_"
    android:parentActivityName=".MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity"/>
</activity>

特別活動

    private static final int NOTIFICATION_ID = 602;
    private static final int REQUEST_CODE_START_ACTIVITY = 610;
    /**
     * Create and show a simple notification containing the received GCM message.
     */
    private void sendNotification(String title, String message, Intent intent) {
        // Create a start PendingIntent
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, REQUEST_CODE_START_ACTIVITY, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

        // Notification properties
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setContentIntent(resultPendingIntent);

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

        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

    }

注意:因為我使用setDefaults(DEFAULT_ALL) ,所以振動許可需要<uses-permission android:name="android.permission.VIBRATE" />

是的,有可能。 您必須在manifest.xml中將活動的“已導出”標志設置為true。

希望能幫助到你。

用這個:

int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent=new Intent(context,YourActivity.class);
PendingIntent  pending=PendingIntent.getActivity(context, 0, intent, 0);
Notification notification;
    if (Build.VERSION.SDK_INT < 11) {
        notification = new Notification(icon, "Title", when);
        notification.setLatestEventInfo(
                context,
                "Title",
                "Text",
                pending);
    } else {
        notification = new Notification.Builder(context)
                .setContentTitle("Title")
                .setContentText(
                        "Text").setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pending).setWhen(when).setAutoCancel(true)
                .build();
    }
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
nm.notify(0, notification);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM