GCM通知单击未打开指定的活动

时间:2015-12-24 07:04:55

标签: android push-notification google-cloud-messaging

我在设备上完美地获得了notification。但它没有打开提到的Activity。在日志中,它会打印上述activity的日志,但会打开MainActivity。已经参考了Android docs中的代码,并根据此处https://stackoverflow.com/a/20744397/1928265的答案更改为代码。但仍然无法正常工作。

private void sendNotification(String title, String body) {

        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent resultIntent = new Intent(this, NotificationCenterActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

        stackBuilder.addParentStack(NotificationCenterActivity.class);

        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT
                );

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setColor(16734816)
                        .setContentTitle(title)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(body))
                        .setDefaults(NotificationCompat.DEFAULT_ALL)
                        .setContentText(body);

        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(resultPendingIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="<package name>"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="22" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.VIBRATE"/>

    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!--<uses-permission android:name="<package name>.permission.permission.RECEIVE" />-->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="<package name>.permission.C2D_MESSAGE" />
    <permission android:name="<package name>.permission.C2D_MESSAGE" android:protectionLevel="signature" />

    <application
        android:name="com.orm.SugarApp"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme"
        tools:replace="android:icon" >
        <meta-data
            android:name="DATABASE"
            android:value="database.db" />
        <meta-data
            android:name="VERSION"
            android:value="1" />
        <meta-data
            android:name="QUERY_LOG"
            android:value="true" />
        <meta-data
            android:name="DOMAIN_PACKAGE_NAME"
            android:value="com.db" />

        <activity
            android:name=".SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait" >
        </activity>

        .
        .
        Other Activities
        .
        .

        <activity
            android:name=".NotificationCenterActivity"
            android:screenOrientation="portrait">
        </activity>

        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />


        <receiver
            android:name="<package name>.GcmBroadcastReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="<package name>" />
            </intent-filter>
        </receiver>

        <service android:name="<package name>.GcmIntentService" />

    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

像这样使用

Random random = new Random();
int mid = random.nextInt(9999 - 1000) + 1000;

Intent notificationIntent = new Intent(context, landingactivity.class);
PendingIntent intent = PendingIntent.getActivity(context, mid, notificationIntent, PendingIntent.FLAG_ONE_SHOT);

Notification.Builder notification = new Notification.Builder(context)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setLargeIcon(largeicon)
            .setSmallIcon(R.drawable.smallicon_push)
            .setTicker(message);

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    notification.setContentIntent(intent);

    mNotificationManager.notify(mid, notification.build());

mid 是用于标识通知的随机数。它应该是收到的所有通知的唯一。

答案 1 :(得分:0)

可能有任何问题。我遇到了同样的问题。 试试这些步骤吧。很高兴,如果有效。

  1. 为您的通知活动添加@Override public void start(Stage primaryStage) { StackPane root = new StackPane(); Pane pane = new Pane(); Circle circle = new Circle(100,100,100); enableDragging(circle); pane.getChildren().addAll(circle); root.getChildren().addAll(pane); circle.setOnDragOver(new EventHandler <DragEvent>() { @Override public void handle(DragEvent event) { /* data is dragged over the target */ System.out.println("onDragOver"); Dragboard db = event.getDragboard(); if(db.hasFiles()){ event.acceptTransferModes(TransferMode.ANY); } event.consume(); } }); circle.setOnDragDropped(new EventHandler <DragEvent>() { @Override public void handle(DragEvent event) { /* data dropped */ System.out.println("onDragDropped"); Dragboard db = event.getDragboard(); if(db.hasFiles()){ for(File file:db.getFiles()){ //String absolutePath = file.getAbsolutePath(); String absolutePath = file.toURI().toString(); Image dbimage = new Image(absolutePath); ImageView dbImageView = new ImageView(); dbImageView.setImage(dbimage); circle.setFill(new ImagePattern(dbimage, 0, 0, 1, 1, true)); } event.setDropCompleted(true); }else{ event.setDropCompleted(false); } event.consume(); } }); Scene scene = new Scene(root, 300, 300); primaryStage.setTitle("Drag and Drop Image from Pc into Circle"); primaryStage.setScene(scene); primaryStage.show(); } private void enableDragging(Node node) { final ObjectProperty<Point2D> mouseAnchor = new SimpleObjectProperty<>(); node.setOnMousePressed( event -> mouseAnchor.set(new Point2D(event.getSceneX(), event.getSceneY()))); node.setOnMouseDragged( event -> { double deltaX = event.getSceneX() - mouseAnchor.get().getX(); double deltaY = event.getSceneY() - mouseAnchor.get().getY(); node.relocate(node.getLayoutX()+deltaX, node.getLayoutY()+deltaY); mouseAnchor.set(new Point2D(event.getSceneX(), event.getSceneY()));; }); } public static void main(String[] args) { launch(args); } }
  2. 更改您的待处理意图android:exported="true" 并在活动清单中设置PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ONE_SHOT);
  3. 将意图过滤器添加到通知活动中。 android:launchMode="singleInstance"