Android CGM收到推送通知后启动Activity

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

收到推送通知后,我想在按下通知时打开À Activity

这是我的服务:

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”。

这是我的 list :

<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 进行了很多研究。

谢谢!

最佳答案

你犯了一个小错误。您正在编写此代码用于打开 Activity 。

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

当您想要打开 Activity 时,您需要使用 getActivity 方法而不是 getBroadcast 方法创建挂起 Intent 。

希望这会有所帮助。

关于Android CGM收到推送通知后启动Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161173/

相关文章:

android - 如何将布局背景合并到工具栏

android - wrap_content 时 TextView 的 dp 大小

java - 我如何从匿名类访问我的主类?

android - GO Locker 的 USER_PRESENT Intent

ios - 当应用程序在后台时使用 React Native 静默 iOS 推送通知

android - 如何为 Android x86 手机编译 Google stressapptest?

android - 在 jpg 和 png 中加载相同的图像会导致不同的位图大小

flutter - 启用或禁用通知抖动

javascript - 从浏览器 : How to feature-detect "Intent" support? 启动 Android 应用程序

Android - GCMRegistrar.checkDevice() 的 UnSupportedOperation 异常