【问题标题】:Android CGM start Activity after receiving Push NotificationAndroid CGM 收到推送通知后启动 Activity
【发布时间】:2016-05-11 11:24:08
【问题描述】:

收到Push Notification后,我想在按下Notification时打开一个Àctivity

这是我的Services

public class GCMIntentService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
    String title = data.getString("title");
    String message = data.getString("message");

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setTicker(message)
            .setAutoCancel(true);

    mBuilder.setDefaults(Notification.DEFAULT_LIGHTS);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);


    Intent myIntent = new Intent(this, MyActivity.class);
    PendingIntent intent2 = PendingIntent.getBroadcast(this, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(intent2);

    notificationManager.notify(1, mBuilder.build());
}



public class GCMIDListenerService extends InstanceIDListenerService {
@Override
public void onTokenRefresh() {
    InstanceID instanceID = InstanceID.getInstance(this);
    String token;
    try {
        token = instanceID.getToken(Resources.getSystem().getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //TODO:send token to the server
  }
}

我只是按照Google 文档完成了所有操作。我前段时间为另一个项目实现了该功能,但有点不同,我使用了我自己的“BroadcastReceiver”而不是Google 提供的“com.google.android.gms.gcm.GcmReceiver”。

这是我的Manifest

<application>

...

 <!-- GCM Push Notifications Config -->
    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="gcm" />
        </intent-filter>
    </receiver>
    <service
        android:name=".push.GCMIntentService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <service
        android:name=".push.GCMIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID" />
        </intent-filter>
    </service>


</application>

有什么想法吗?我错过了什么吗?

这里有一些我遵循的文档:

Google official documentation

CGM tutorial By Dustin Rothwell

当然,我在stackoverflow 进行了很多研究。

谢谢!

【问题讨论】:

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


    【解决方案1】:

    你犯了一个小错误。您正在为打开活动编写此代码。

    PendingIntent intent2 = PendingIntent.getBroadcast(this, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(intent2);
    

    当您想要打开活动时,您需要使用getActivity 方法而不是getBroadcast 方法来创建待处理意图。

    希望这会有所帮助。

    【讨论】:

    • 太棒了!非常感谢:)
    猜你喜欢
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多