1

我为GCM推送通知编写了IntentService。 我收到消息,但显示我的通知给用户时出现了问题。 这里是我的代码:Android通知未显示

import com.google.android.gms.gcm.GoogleCloudMessaging; 
import android.app.IntentService; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.app.NotificationCompat; 
import android.support.v4.app.TaskStackBuilder; 
import android.util.Log; 

public class GcmIntentService extends IntentService { 
    public static final int NOTIFICATION_ID = 1; 


    public GcmIntentService() { 
     super("GcmIntentService"); 
    } 

    public static final String TAG = "GCM test"; 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
     String messageType = gcm.getMessageType(intent); 
     if (!intent.getExtras().isEmpty()) { // has effect of unparcelling Bundle 
      if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
       sendNotification("Send error: " + intent.getExtras().toString()); 
      } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { 
       sendNotification("Deleted messages on server: " + intent.getExtras().toString()); 
       // If it's a regular GCM message, do some work. 
      } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
       // Post notification of received message. 
       sendNotification("message:\n" + intent.getStringExtra("message")); 
       Log.i(TAG, "Received: " + intent.getExtras().toString()); 
      } 
     } 
     // Release the wake lock provided by the WakefulBroadcastReceiver. 
     GcmBroadcastReceiver.completeWakefulIntent(intent); 
    } 

    private void sendNotification(String msg) { 
     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.ic_notif) 
         .setContentTitle("My notification") 
         .setContentText(msg); 
     Intent resultIntent = new Intent(this, PopupMessageActivity.class); 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addParentStack(PopupMessageActivity.class); 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = 
       stackBuilder.getPendingIntent(
         0, 
         PendingIntent.FLAG_UPDATE_CURRENT 
       ); 
     mBuilder.setContentIntent(resultPendingIntent); 
     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    } 
} 

我没有看到这个错误。我的意思是,我复制了Androids developer guide的代码。

此代码所做的唯一事情就是在手机的通知栏中显示小图标(本例中为“ic_notif”)。 但是没有弹出给用户的文本或通知。

我使用android-studio。 我的调试设备是一个带有android 4.0.3(API 15)的huawai u8666e。 至少我想这个API级别是我的这个应用程序的最低要求。

+0

如果您拖动通知栏,该怎么办?文字是否显示在那里? “不......通知......弹出”是什么意思? –

+1

当我向下拖动通知栏时,文本正在显示。这是一个正常的行为为Android?对不起,我是一名iPhone用户,并且是android开发人员的新手。我认为,如果你与建设者建立一个消息,就像在iOS屏幕上的弹出窗口... – user4878754

+0

是的,这是正常的和正确的。我会建议尽快阅读更多有关Android UI设计模式的文章。这两个系统之间有很多不同之处,Android用户不想看到iOS设计模式(反之亦然)。我已经在下面解释了一些。 –

回答

1

你看到的是正常设计的Android行为在棒棒糖之前的版本。

设计逻辑是这种方法创建一个更干净的界面,并且不会通过在他们面前放置弹出框来中断用户的当前操作。 (有很多关于哪种方法更好的争论 - iOS弹出窗口与Android通知)。

当创建Notification时,棒棒糖通过在设备窗口顶部创建一个小的弹出窗口来稍微改变这一点。


如果你真的想强制显示一个弹出对话框,你应该看看设计一个“全屏”通知。

看到Android开发者文档:

使用这种方法,你可以创建一个新Activity你想要的任何自定义布局,并推出,与其把通知中状态栏。

(全面实施的全屏通知将超出这篇文章的范围)


我会建议不要强迫除了在极少数情况下,如闹钟全屏通知,或电话应用程序。相反,我会建议你坚持Android设计和操作系统的方式。

+1

感谢这个有用的答案。 – user4878754