应用程序关闭时,GCM Broadcast接收器不会解除gcm通知

时间:2016-05-13 09:37:23

标签: java android google-cloud-messaging broadcastreceiver

我有一个广播接收器,当应用程序处于打开状态并且在后台时检测到即将来临的通知但是当最近的应用程序被清除时接收器无法正常工作时请向我提出建议。

<receiver
    android:name="com.twogether.receivers.GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
      <intent-filter>
       <action android:name = "com.google.android.c2dm.intent.RECEIVE" />
       <category android:name="com.google.android.gcm.demo.app" />
      </intent-filter>
</receiver>

明显的声明:

#make slices
slices <- c(19, 26, 55)

# Define some colors 
colors <- c("yellow2","olivedrab3","orangered3")

# Calculate the percentage for each day, rounded to one decimal place
slices_labels <- round(slices/sum(slices) * 100, 1)

# Concatenate a '%' char after each value
slices_labels <- paste(slices_labels, "%", sep="")

# Create a pie chart with defined heading and custom colors and labels
pie(slices, main="Sum", col=colors, labels=slices_labels, cex=0.8)

# Create a legend at the right   
legend("topright", c("DH","UT","AM"), cex=0.7, fill=colors)

2 个答案:

答案 0 :(得分:0)

尝试在清单中注册您的接收者,使用GcmListenerService接收消息。

google example https://github.com/googlesamples/google-services就是这样做的例子。

    <!-- gcm_receiver, this is android source-->
    <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="com.myapp.gcm" />
        </intent-filter>
    </receiver>

    <!-- gcm_listener service -->
    <service
        android:name="com.qblinks.qmote.GcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

//使用此服务接收消息

public class GcmListenerService extends com.google.android.gms.gcm.GcmListenerService {
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.v(TAG, "From: " + from);
        Log.v(TAG, "Message: " + message);
        sendNotification(message);
}

private void sendNotification(String message) {

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle("this is title")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))//multi line
            .setSound(defaultSoundUri); // ring or vibrate if no ring
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(Const.NOTIFICATION_GCM /* ID of notification */, notificationBuilder.build());
}

}

答案 1 :(得分:0)

当应用程序关闭时,您需要唤醒应用程序,意味着您需要获得执行此操作的权限

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

然后你可以设置:

AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        manager.set(AlarmManager.RTC, System.currentTimeMillis() + 5000, yourIntent);